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

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',

Replies are listed 'Best First'.
Re^7: Saving sql results to an array all rows?
by Krambambuli (Curate) on Aug 20, 2008 at 13:07 UTC
    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
      Krambambuli, thanks for the reply once again. I think we are getting closer. With the code that you suggested above I now get the following results, but once again they are not getting sorted at all not sure why. I even went ahead and added a sort with the code shown below. thanks for the help I really appreciate it, I think i've been looking at this so long that I am just running in circles. Results
      $VAR1 = '2008-07-26 10.10.10.1 3.3.3.3 80 hxxp://3.3.3.3 200 OK'; $VAR14 = '2008-07-26 192.168.1.32 11122 3.3.3.3 80 hxxp://4.4.4.4 200 +OK'; $VAR29 = '2008-07-26 1.1.1.1 11122 3.3.3.3 80 hxxp://3.3.3.3 200 OK';
      Current code
      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 @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; 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] ) ] } @sorted1; my @all_rows1 = map { [ split( /\s+/, $_ ) ] } @sorted; print Dumper ( @sorted ); }
        As Krambambuli noted, the @sorted1 code is correct and the @sorted isn't. In the wrong one, you're applying split to an array reference.

        Seeing as how you are sorting ip adresses, you could simplify this sort routine using Socket's function inet_aton.

        #!/usr/bin/perl use strict; use warnings; use Socket; use Data::Dumper; my @data = map{[split]} <DATA>; print Dumper \@data; my @sorted = map{$_->[0]} sort {$a->[1] cmp $b->[1]} map { [$_, inet_aton($_->[1]) ]} @data; print Dumper \@sorted; __DATA__ alpha 2.23.14.71 beta 10.23.14.71 gamma 25.26.1.6 delta 1.2.3.4 epsilon 10.24.14.71
        Socket is included with perl 5.8.8 at least, and likely for earlier versions of perl.
        Delete the

        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] ) ] } @sorted1; my @all_rows1 = map { [ split( /\s+/, $_ ) ] } @sorted; print Dumper ( @sorted );
        part completely and dump or print @sorted1, where you have the results you want, but which you then ignore just to rebuild and reuse the same buggy results that you started with.

        If it still doesn't work: try to understand and comment each line in the code before using it. You'll definitely get it right by your own! ;)

        Krambambuli
        ---