Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array sorting syntax question 1

Status
Not open for further replies.

tonykent

IS-IT--Management
Jun 13, 2002
251
GB
Can someone assist me with getting the correct syntax to sort this array please? I've tried all sorts and searched past threads but have not found the right answer.

I have a very complex and lengthy script that queries a database and eventually ends up with me having data in this format (this is 3 lines of data below - it doesn't display well so I have prefixed each line for clarity):

Code:
<line1> MOVE_top\general\major\captain\corp%1@MOVE_top%2.0_BL.01   corp%1:dir:pit01t     MOVE_top\general\major\captain
<line 2> MOVE_top\general\major%1@MOVE_top%2.0_BL.01                major%1:dir:pit01t    MOVE_top\general
<line3> MOVE_top\general\major\captain%1@MOVE_top%2.0_BL.01        captain%1:dir:pit01t  MOVE_top\general\major

I need to sort the lines based on the third field in each line.

Code:
#!/usr/contrib/bin/perl
use strict;
my @array;
my $x;
my $p=1;
while (<DATA>) {
  chomp;
  ($array[$p][1], $array[$p][2], $array[$p][3],) = split /\s+/;
  $p++;
}
close DATA;

for ($x=1;$x<$p;$x++) {
        #<do processing here: print to show line order>
	print "$array[$x][3]\n";
}

__DATA__
MOVE_top\general\major\captain\corp%1@MOVE_top%2.0_BL.01   corp%1:dir:pit01t     MOVE_top\general\major\captain
MOVE_top\general\major%1@MOVE_top%2.0_BL.01                major%1:dir:pit01t    MOVE_top\general
MOVE_top\general\major\captain%1@MOVE_top%2.0_BL.01        captain%1:dir:pit01t  MOVE_top\general\major

The code above simulates the data structure I have i.e $array[$p][1] $array[$p][2] $array[$p][3] for each line.

It currently gives the following output:

Code:
MOVE_top\general\major\captain
MOVE_top\general
MOVE_top\general\major

I need to sort each line based on $array[$p][3]so that it displays:

Code:
MOVE_top\general
MOVE_top\general\major
MOVE_top\general\major\captain

Can anyone help with the syntax to do this?

 
Code:
print "$_\n" for sort map $array[$_][3], ( 1 .. $p - 1 );
 
Wow, Ishnid, that was fast :)

That works a treat. Moving on from there, though, how would I write the old array into a new (sorted) array?
 
It makes it easier if you start your arrays with index 0 (which is the usual way - is there some particular reason you're using 1?), rather than 1. You could do it like this:
Code:
@array = sort { $a->[3] cmp $b->[3] } @array;

If there's a reason for not using the first space in the array, you could do this to order everything from the second element on (i.e. from index 1 on). It's not very pretty, though :(
Code:
@array[1..$#array] = sort { $a->[3] cmp $b->[3] } @array[1..$#array];
 
Thanks very much Ishnid. The last bit of code above was exactly what I needed - no wonder I didn't manage to come up with it myself.
 
No problem.

I still think you should be beginning your array at 0 rather than 1, as that's the normal way arrays work. Even when you try to start using it at 1, there's still a position 0 which just doesn't have a value. The code becomes much cleaner (and a tiny bit more memory-efficient) that way. For example:
Code:
#!/usr/contrib/bin/perl
use strict;
my @array;
my $x;

while (<DATA>) {
  chomp;
  # split and add the result to the array
  push @array, [ split /\s+/ ];
}
close DATA;

# sort the array
@array = sort { $a->[2] cmp $b->[2] } @array;

for ( @array ) {
   print "$_\n";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top