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

Krambambuli I followed your example and I get all rows/results correctly. Now the only problem/thing that I do not know how to deal with is the @{$row_ref} array? If I wanted to run the sorted code which runs against an array passing it the @{$row_ref} array does not work. I get an array code ARRAY(0x9019330) on the screen here is the code that I am using currently. Thanks for the help!!
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); } foreach my $row_ref (@all_rows) { print "@{$row_ref}\n"; } }
Here is the sorting code that I use in a separate script which is what I am having trouble incorporating to the @{$row_ref} array. I am guessing that I can't use @{$row_ref} name when running my sort? Any ideas or help would be greatly appreciated.
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])]} @lines; print "$_\n" for @{$row_ref};
sample of the sql output below. 2008-07-26 10.168.1.1 11122 3.3.3.3 80 hxxp://3.3.3.3 200 OK

Replies are listed 'Best First'.
Re^3: Saving sql results to an array all rows?
by Krambambuli (Curate) on Aug 18, 2008 at 19:27 UTC
    Try to see if the following code might help you:
    use strict; use warnings; use Data::Dumper; my @lines = ( '2008-07-26 13.168.1.1 11122 3.3.3.3 80 hxxp://3.3.3.3 200 OK' +, '2008-07-26 10.169.1.1 11122 3.3.3.3 80 hxxp://3.3.3.3 200 OK' +, '2008-07-26 10.168.2.1 11122 3.3.3.3 80 hxxp://3.3.3.3 200 OK' +, '2008-07-26 10.168.1.1 11122 3.3.3.3 80 hxxp://3.3.3.3 200 OK' +, ); 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] ) ] } @lines; my @all_rows = map { [ split( /\s+/, $_ ) ] } @lines; 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_rows; print Dumper( @sorted1 );

    Krambambuli
    ---
      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"; }
        And what happens if you replace your
        my @sorted =...
        with the replacement I've offered in the previous snippet... ?

        Krambambuli
        ---