Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I want to fill some arrays with the content of some lookup arrays, but I don't know how to get the right array. Some code:
use DBI; my @tables = qw(name car pet); my @name, @car, @pet; sub fill_array() { $sql = "select * from $i;"; $dbh = DBI->connect($dsn, $dbuser, $dbupass) || die "Connection failed: $!\n"; $sth = $dbh->prepare( "$lsql;" ) || die "Couldn't prepare: $!\n"; $sth->execute(); while ( my @row = $sth->fetchrow_array ) { #Here's my problem :( <@(right_array)> = (<@(right_array)>, row[1]); } $rc = $dbh->disconnect || die "Disconnect Failed: $!\n"; }
Thanks!

Replies are listed 'Best First'.
Re: Fill Arbitrary Array
by tadman (Prior) on May 02, 2002 at 08:24 UTC
    Maybe you mean something like this, where you are adding a value to the @right_array listing:
    push (@right_array, $row[1]);
    Or, something like this where you are adding the row to the @right_array:
    push (@right_array, \@row);
    Probably what you mean by @right_array is that you want to specify which array to put the stuff into, correct?
    sub fill_array(\@) { my ($right_array) = @_; # ... while (my @row = $sth->fetchrow_array()) { push (@$right_array, \@row); } # ... } # As a test fill_array (@name);
    This is achived by using references. Instead of knowing exactly which array you are going to be using, you have a handle to it ($right_array versus @right_array) that you can use like an array with the proper prefix (@$right_array).

    If you choose to put the entire row in there, then what you have is a multi-dimensional array. To get out data, you can either get a whole row (array) or a single entry:
    my @row = @{$name[1]}; # Row 1 my $row_item_1 = $name[1][1]; # Row 1 column 1
    Maybe this is what you had in mind?

    Don't forget that array numbering starts at 0, so the first element is actually $row[0]. Call it the "zeroth" row, if you will.

    Something to consider apart from this code is to use some of the specialized DBI calls, such as selectall_arrayref and selectcol_arrayref. For the latter, in particular, there is no point in fetching a whole row of data when all you want is a single column.
      I don't know which arrays are filled with this code :(
      sub fill_array(\@) { my ($right_array) = @_; # ... while (my @row = $sth->fetchrow_array()) { push (@$right_array, \@row); } print "Values: @$right_array\n"; #Works! } fill_array(@name); foreach my $entry (@name) { print "$entry\n"; #Remains empty... }
        It should fill whatever array you pass to it, which in this case is @name. The reason this can be done is because of the way the sub is declared, in particular, the (\@) definition means that the first parameter is an array reference. This way, instead of passing a copy of the @name array, a reference to it is passed, allowing you to manipulate it.

        With that in mind, I'm surprised that you get "nothing". @name should be filled with array references, these sub-arrays that contain the various rows. Using your code, since you are not de-referencing them, you should see something like this:
        ARRAY(0x80fe5a8) ARRAY(0x80fe5d2) ARRAY(0x80fe6f4)
        The reason is you are printing an array reference as a string, which isn't going to work. A solution is to either use the venerable Data::Dumper or just deference it:
        # Data::Dumper saves the day again use Data::Dumper; print Dumper (\@name); # De-reference it and treat it as an array foreach my $row (@name) { print join (',', @$row),"\n"; }
        I'm not sure how familiar you are with references, but here's a 5 second intro:
        my @array = qw[ 1 2 3 ]; my $array_ref = \@array; # Backslash makes a reference my @array_copy = @$array_ref; # @ de-references array reference $array[2] = 4; # Modifies @array directly print $array_ref->[2]; # Should be '4' now $array_ref->[1] = 5; # Modifies @array by reference print $array[1]; # Should be '5' $array_copy[1] = 6; # Modifies @array_copy, not @array print $array[1]; # Still '5'
      Indeed, what I meant with the right array is that I want to specify which array is going to be used. To for lookup-table "name" I want to use array @name. You're right about the selection, I just need one column.