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 Issue

Status
Not open for further replies.

Extension

Programmer
Joined
Nov 3, 2004
Messages
311
Location
CA
Hi,

I have a file in the following format:

Code:
FIELD1|FIELD2|FIELD3|FIELD4
1|90-09|LINK|A-D
2|05-09|LINK-PL|FDX

Now I'm converting the file to an array and then I'm splitting the fields. I would to sort the array by the "FIELD2".

Code:
	open(FILE,"file.dat") || die("Error !")		
	@File = map {chomp; [split /\|/, $_]} <FILE>;
	close(FILE);
	
	sort { $b->[1] cmp $a->[1] } @File;

The array is not getting sorted properly with my current data.
 
Try:
Code:
@File = sort { $b->[1] cmp $a->[1] } @File;
The "sort" function doesn't sort in-place.
 
I'd look at dumping that in a hash, and then sort the hash, and to account for possible dupes, and the (what looks like a) unique identifier.
Code:
while (<FILE>) {
  @data=split /\|/, $_;
  $hash{$data[0]."-".$data[2]}=$_;
}
close FILE;
foreach $key (sort(keys(%hash))) {
    push (@array, $hash{$key}
}

Not tested BTW


Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
push (@array, $hash{$key});
oops

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thank you ishnid. Learned something new ! Thanks again for your help.

Dumb question regarding arrays:
The first value of an array is always 0; I would like to know if there is an easy way to make it record #1 instead of 0 ?

Thank you
 
Thanks PaulTEG for your advice.

I will have to start using hashes more frequently as they are more easy to process and to manipulate with large amount of data. Thank you.

 
From
# $[

The index of the first element in an array, and of the first character in a substring. Default is 0, but you could theoretically set it to 1 to make Perl behave more like awk (or Fortran) when subscripting and when evaluating the index() and substr() functions. (Mnemonic: [ begins subscripts.)

As of release 5 of Perl, assignment to $[ is treated as a compiler directive, and cannot influence the behavior of any other file. (That's why you can only assign compile-time constants to it.) Its use is highly discouraged.

Note that, unlike other compile-time directives (such as strict), assignment to $[ can be seen from outer lexical scopes in the same file. However, you can use local() on it to strictly bind its value to a lexical block.

But, don't ... don't ... don't ... ever ... do that. All I can say about it.
 
^^^ I'm with him/her

If in life, you learn to walk around obstacles, rather than right through, it exemplifies diplomacy, what we know as a good skill, to the point of appointment, and then .... it all goes horribly wrong ... or you could just take our word for it

do not, (!==doughnut) prick with $[, because of what may or may not happen ... the choice, as they say, is yours ...

--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top