in reply to Fill array

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.

Replies are listed 'Best First'.
Re: Re: Fill Arbitrary Array
by Anonymous Monk on May 02, 2002 at 13:55 UTC
    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'
Re: Re: Fill Arbitrary Array
by Anonymous Monk on May 02, 2002 at 09:53 UTC
    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.