开发者

Sorting data based on the third column

开发者 https://www.devze.com 2023-02-17 12:56 出处:网络
AAAAAAAA0.0.0.0hs01.stuff.net BBBBBBBB0.0.0.0hs01.morestuff.net CCCCCCCC0.0.0.0hs01.evenmorestuff.net DDDDDDDD0.0.0.0hs01.stuff.net
AAAAAAAA    0.0.0.0   hs01.stuff.net
BBBBBBBB    0.0.0.0   hs01.morestuff.net
CCCCCCCC    0.0.0.0   hs01.evenmorestuff.net
DDDDDDDD    0.0.0.0   hs01.stuff.net
EEEEEEEE    0.0.0.0   hs01.stuff.net
FFFFFFFF    0.0.0.0   hs01.evenmorestuff.net
GGGGGGGG    0.0.0.0   hs01.stuff.net
HHHHHHHH    0.0.0.0   hs01.evenmorestuff.net

I have been searching all over but I just couldn't find the answer I am looking for.

How do you sort by Column 3 of the output above?

This is my code:

#!/usr/local/bin/perl
use warnings;
use DBI;
use DBD::Oracle;
use HTML::Template;
use List::MoreUtils 'uniq';

######################### Open File and Split The Data Into An Array ####################################
$input_data_file = 'C:\wamp\www\input_corrolation_file.txt';
open (DAT, $input_data_file)
      or die ("Could not open file!");

@raw_data = <DAT>;

close(DAT);
#########################################################################################################


$dbh_source2 = DBI->connect("dbi:Oracle:host=????;port=????;sid=????",'????','????');
$SEL = "SELECT DISTINCT PE_LOOPBACK_IP,PE_FQDN FROM TABLE_NAME WHERE SITE_NAME = ?";
$sth = $dbh_source2->prepare($SEL);
print '<table border=1>';
print '<tr>';
print '<th>开发者_如何学编程Tower name</th>';
print '<th>SUR IP</th>';
print '<th>SUR FQDN</th>';
print '</tr>';



foreach my $data_line (@raw_data) {
        chomp $data_line;
       $sth->execute($data_line);

        my @row = $sth->fetchrow_array;
        unshift (@row, $data_line);
              #Print data into cells#
              print "<tr>";
              foreach (@row) {
                print "<td>$_</td>";
              }
              print "</tr>";
              #print "<$data_line>\t @row\n";



}

print "</table>";
END {
            $dbh_source2->disconnect if defined($dbh_source2);
}

So how would I be able to sort the data in @row by the third column?

All help is appreciated!


Separate the fetching from the database and the printing, and sort before you print:

my @rows;
foreach my $data_line (@raw_data) {
    chomp $data_line;
    $sth->execute($data_line);

    my @row = $sth->fetchrow_array;
    unshift (@row, $data_line);
    push @rows, \@row;
}

@rows = sort { $a->[2] cmp $b->[2] } @rows;

foreach my $row (@rows) {
    print "<tr>";
    foreach (@$row) {
        print "<td>$_</td>";
    }
    print "</tr>";
}


You can put the table into a hash where the keys are the lines and the values are the values of the fields you want to sort by, and then sort the hash by the values:

my @arr;
my @sorted_arr;
my $key;
my %hash_sort;
my @broken_line;

push (@arr,"last\t222222\tzrf.bcd.cde");
push (@arr,"first\t999999\tabc.poi.pko");
push (@arr,"second\t444444\tjko.drt.xdf");
push (@arr,"third\t111111\tmno.lkn.tyf");

foreach $key (@arr){
  # print "$key\n";
  @broken_line = split("\t",$key);      
  $hash_sort{$key}=$broken_line[2];
}

foreach $key (sort {$hash_sort{$a} cmp $hash_sort{$b}} (keys(%hash_sort))){
  print "$key\n";
  push (@sorted_arr,$key);
}

this code creates a new sorted array, as well as prints the sorted lines.

0

精彩评论

暂无评论...
验证码 换一张
取 消