in reply to How can I sort this data on the first field

Since your numbers are the initial field, and are terminated by non-numbers, you can just treat the whole string as a number and Perl will Do The Right Thing:
print sort {$a <=> $b} <DATA>; __DATA__ 30|microsoft 70|aol 76|netscape 5|trouble? 35|mozilla 40|opera
No need for splitting or Schwartzian Transforms.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: How can I sort this data on the first field
by NateTut (Deacon) on Apr 25, 2005 at 20:30 UTC
    Roy Johnson's solution works, but it is not warning free. The original problem seems to me to scream out for the use of a hash. Here is how I would have approached the problem:
    use strict; use warnings; print "Content-type: text/html\n\n"; my %someData; while(<DATA>) { chomp(); (my $key, my $value) = split(/\|/, $_, 2); $someData{$key} = $value; } foreach (sort { $a <=> $b } keys %someData) { print "$_\{$someData{$_}\}\n"; } __DATA__ 30|microsoft 70|aol 76|netscape 5|trouble? 35|mozilla 40|opera 100|FireFox

      It's not a hash that you want... it's probably an array of arrays. What if there were duplicate keys? your hash will eat them up and you'll end up with only the last entry.

      compare:
      print "With Hash:\n\n"; $save = tell DATA; while(<DATA>) { my ($k, $v) = split '\|'; $hash{$k} = $v; } print "$_|$hash{$_}" for sort { $a <=> $b } keys %hash; seek DATA, $save, 0; print "\n\nWith array:\n\n"; while(<DATA>) { my ($k, $v) = split '\|'; push @arr, [$k, $v]; } print "$_->[0]|$_->[1]" for sort { $a->[0] <=> $b->[0] } @arr; __DATA__ 30|microsoft 70|aol 76|netscape 5|trouble? 35|mozilla 30|die microsoft 40|opera 100|FireFox 5|no more trouble

      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
      Chady | http://chady.net/
      Are you a Linux user in Lebanon? join the Lebanese GNU/Linux User Group.