×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

sort not sorting

sort not sorting

sort not sorting

(OP)
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;





RE: sort not sorting

Hi

There are multiple problems.
  • You can not use $0 and similar variables in my statements.
  • You can not assign value to $0 and similar variables.
  • If you not need to properly assign all those values to variables, use undef instead.
  • Assigning scalar to array will make the array have a single element.
  • Sorting a single element array is not really spectacular. You should collect the values and sort outside the while loop.

CODE --> Perl

my @machs;

while (<DATA>) {
  my ( undef, undef, undef, undef, $machine_name ) = split( /:/, $_);
  push @machs, $machine_name;
}

my @sortedmachs = sort { $a cmp $b } @machs;
print "@sortedmachs\n";

__DATA__
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 

Next time please post your code between [code] and [/code] TGML tags.

Feherke.
http://feherke.github.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close