in reply to Re^3: Saving sql results to an array all rows?
in thread Saving sql results to an array all rows?

Hey thanks again for the reply. I think that I may not be explaining myself in the best way, but here is the code that I have currently.
my @lines = read_file('/tmp/test.txt'); chomp(@lines); my $sql = q|select * from table_name where srcaddr = ? or dstaddr = ?| +; my $sth = $dbh->prepare($sql) or die "Can't prepare SQL statement: $DB +I::errstr\n"; foreach my $line (@lines) { $sth->execute($line, $line) or die "Can't execute SQL statemen +t: $DBI::errstr\n"; my @all_rows; while ( my $ref = $sth->fetchrow_arrayref()) { push(@all_rows, $ref); } my @sorted = map {$_->[0]} sort {$a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] || $a->[4] <=> $b->[4]} map { [$_, split(/\./, (split /\s+/, $_)[1])]} @all_rows; print "$_\n" for @sorted; }
The problem is that whenever I run the code I get the following shown below. I am guessing this is because I am using the array @all_rows which it doesn't know how to handle? I am saving the output/results from the SQL query to an array called @all_rows, now what I want to do is sort the data in column 2 in that array. What is the proper way to work/sort the data in the array @all_rows which contains all the sql results. Thanks again for the help..
ARRAY(0x96b0d80) ARRAY(0x96b0d80) ARRAY(0x96b0d80) ARRAY(0x96b0d80) ARRAY(0x96b0d80) ARRAY(0x96b0d80) ARRAY(0x96b0d80) ARRAY(0x96b0d80) ARRAY(0x96b0d80)
FYI if I print the @all_rows array using a foreach loop I get the proper results
foreach my $row_ref (@all_rows) { print "@{$row_ref}\n"; }

Replies are listed 'Best First'.
Re^5: Saving sql results to an array all rows?
by Krambambuli (Curate) on Aug 19, 2008 at 16:12 UTC
    And what happens if you replace your
    my @sorted =...
    with the replacement I've offered in the previous snippet... ?

    Krambambuli
    ---
      Hey Krambambuli, thanks again for helping me out. Here is the code that I have with your suggestions(FYI I changed the line in your code that shows @lines to @all_rows and @all_rows to @all_rows1, since the array @lines is storing the data which you manually populated).
      Below the code is a sample of the results. Two questions, the @lines array in your example shows the data correctly as I would like to work with, the only problem is that they are not stored in 1 line in the array.
      foreach my $line (@lines) { $sth->execute($line, $line) or die "Can't execute SQL statemen +t: $DBI::errstr\n"; my @all_rows; while ( my $ref = $sth->fetchrow_arrayref()) { push(@all_rows, $ref ); } my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] || $a->[4] <=> $b->[4] } map { [$_, split(/\./, (split /\s+/, $_)[1] ) ] } @all_rows; my @all_rows1 = map { [ split( /\s+/, $_ ) ] } @all_rows; my @sorted1 = map { join( ' ', @{ $_->[0] } ) } sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] || $a->[4] <=> $b->[4] } map { [$_, split(/\./, $$_[1] ) ] } @all_rows1; print Dumper @sorted1; }
      Here are the results that I am getting with the code shown above looks like I am just getting array refs??
      $VAR1 = 'ARRAY(0x94db6f8)'; $VAR2 = 'ARRAY(0x94db6f8)'; $VAR3 = 'ARRAY(0x94db6f8)'; $VAR1 = 'ARRAY(0x94db6f8)'; $VAR2 = 'ARRAY(0x94db6f8)'; $VAR3 = 'ARRAY(0x94db6f8)'; $VAR4 = 'ARRAY(0x94db6f8)'; $VAR5 = 'ARRAY(0x94db6f8)'; $VAR6 = 'ARRAY(0x94db6f8)'; $VAR1 = 'ARRAY(0x94db6f8)'; $VAR1 = 'ARRAY(0x94db6f8)'; $VAR1 = 'ARRAY(0x94db6f8)';
      Here is what the results look like if I used the code which I posted in the previous post(keep in mind that the results that I am getting with that code are correct/accurate) but the way it gets printed the sort won't do anything cause the sort is looking for data the look like the data in your @lines example that you used in your sort map.
      $VAR1 = [ 'data', 'data', 'data', 'data', 'data', ]; $VAR2 = [ 'data', 'etc..etc',
        Oh well... :)

        Just make that
        foreach my $line (@lines) { $sth->execute($line, $line) or die "Can't execute SQL statemen +t: $DBI::errstr\n"; my @all_rows; while ( my $ref = $sth->fetchrow_arrayref()) { push(@all_rows, $ref ); } # my @sorted # = map { $_->[0] } # sort { $a->[1] <=> $b->[1] # || # $a->[2] <=> $b->[2] # || # $a->[3] <=> $b->[3] # || # $a->[4] <=> $b->[4] # # } map { [$_, split(/\./, (split /\s+/, $_)[1] ) ] } # @all_rows; # # my @all_rows1 = map { [ split( /\s+/, $_ ) ] } # @all_rows; # my @sorted1 = map { join( ' ', @{ $_->[0] } ) } sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] || $a->[4] <=> $b->[4] } map { [$_, split(/\./, $$_[1] ) ] } # @all_rows1; @all_rows; # print Dumper @sorted1; print Dumper ( \@sorted1 ); }

        Krambambuli
        ---
        enjoying Mark Jason Dominus' Higher-Order Perl