foreach my $line (@lines) { $sth->execute($line, $line) or die "Can't execute SQL statement: $DBI::errstr\n"; my @all_rows; while ( my $ref = $sth->fetchrow_arrayref()) { push(@all_rows, $ref); my @sorted = map{$_->[0]} sort {$a->[1] cmp $b->[1]} map { [$_, inet_aton($_->[1]) ]} @all_rows; print Dumper \@sorted; } } -------------------------------------------------------- Results: FYI there are three entries in the file which is save to the @line array so three results shown below. $VAR1 = [ [ '2008-07-26', '10.10.10.1' ], $VAR1->[0], $VAR1->[0], ]; $VAR1 = [ [ '2008-07-26', '192.168.1.32' ], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0] ]; $VAR1 = [ [ '2008-07-26', '1.1.1.1' ], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0], ];