grazinggoat
Programmer
I am attempting to sort the contents of a file by the 5th position.
the file is in this order:
0:1:2:3:server1a2:5:6:7
0:1:2:3:server4a2:5:6:7
0:1:2:3:server8a2:5:6:7
0:1:2:3:server1a2:5:6:7
it should output:
server1a2
server1a2
server4a2
server8a2
But it returns the same order that in the infile.
Here is what I have coded from all I have read this should
work...
#!/usr/bin/perl
my $infile = "/home/user/mach.txt";
open(FILE, $infile) or die "Can't open file";
while(<FILE>){
my ( $0, $1, $2, $3, $machine_name,
$5, $6, $7) = split( /:/, $_);
my @machs = $machine_name;
my @sortedmachs = sort { $a cmp $b } @machs;
print "@sortedmachs\n";
}
close FILE;
the file is in this order:
0:1:2:3:server1a2:5:6:7
0:1:2:3:server4a2:5:6:7
0:1:2:3:server8a2:5:6:7
0:1:2:3:server1a2:5:6:7
it should output:
server1a2
server1a2
server4a2
server8a2
But it returns the same order that in the infile.
Here is what I have coded from all I have read this should
work...
#!/usr/bin/perl
my $infile = "/home/user/mach.txt";
open(FILE, $infile) or die "Can't open file";
while(<FILE>){
my ( $0, $1, $2, $3, $machine_name,
$5, $6, $7) = split( /:/, $_);
my @machs = $machine_name;
my @sortedmachs = sort { $a cmp $b } @machs;
print "@sortedmachs\n";
}
close FILE;