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

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 ); }

Replies are listed 'Best First'.
Re^9: Saving sql results to an array all rows?
by Cristoforo (Curate) on Aug 20, 2008 at 20:45 UTC
    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.
      Cristoforo thanks for the reply, however when using the code you provided I get the same results correct results but unsorted. Any ideas? I changed my query to only return two rows so it would closely follow your example. Below are the results. Thanks
      my @data = map { [$_, split(/\./, (split /\s+/, $_)[1])]} @al +l_rows; #print Dumper @data; my @sorted = map{$_->[0]} sort {$a->[1] cmp $b->[1]} map { [$_, inet_aton($_->[1]) ]} @data; print Dumper @sorted; } Results $VAR1 = [ [ '2008-07-26', '13.168.1.1' ] ]; $VAR2 = [ $VAR1->[0] ]; $VAR1 = [ [ '2008-07-26', '10.169.1.1' ] ];
        Well, you're almost there. You don't need the code line

        my @data =  map { [$_, split(/\./, (split /\s+/, $_)[1])]} @all_rows;

        That isn't necessary. I don't know why you did that! You transformed @all_rows using the same buggy code Krambambuli noted in his last post. Try the code below, without the transformation.

        my @sorted = map{$_->[0]} sort {$a->[1] cmp $b->[1]} map { [$_, inet_aton($_->[1]) ]} @all_rows; print Dumper \@sorted;

        (Also, leave the backslash before print Dumper \@sorted;. It's there to print out the array as a whole using Data::Dumper. Did you paste my example code and run it?)

Re^9: Saving sql results to an array all rows?
by Krambambuli (Curate) on Aug 20, 2008 at 17:06 UTC
    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
    ---