in reply to What is sprintf doing?
If your real data is not the same as the sample data that you posted, then you will need to adjust the code I offered to suit.
The basic problem is that you want to sort your strings according to a numeric field embeded part way through your strings. To do that, you need to extract the numeric value from each string in order to compare them. In the sample data you supplied, the numeric field followed a (unique) comma and a space, so extracting that using a regex was easy.
$string =~ m[ , # a comma \s+ # followed by at least one space ( # capture \d+ # one or more digits ) ];
In the following code, I've done two things that hopefully will clarify what is going on.
#! perl -slw use strict; my @array = split '\n', <<'EOA'; Item1 - 2 foo, 2 bar Item2 - 0 foo, 1 bar Item3 - 1 foo, 3 bar Item4 - 1 foo, 2 bar EOA my @sorted = map{ # Strip the first five characters that we added earlier. substr $_, 5; } sort { # make the sort descending $b cmp $a } map { # print out the modified record to show what sprintf is doing print "before sort: '$_'"; $_; } map { ## try the regex and die displaying ## the failing record if it doesn't match die "Regex did not match: '$_'" unless m[,\s+(\d+)]; # It matched, so $1 contains the value to prepend. # pad to width 5. # use a bigger value here and above in the substr # if your numbers can be bigger than 99999. sprintf '%05d%s', $1, $_; } @array; print $/; print for @sorted; __END__ P:\test>test2 before sort: '00002Item1 - 2 foo, 2 bar' before sort: '00001Item2 - 0 foo, 1 bar' before sort: '00003Item3 - 1 foo, 3 bar' before sort: '00002Item4 - 1 foo, 2 bar' Item3 - 1 foo, 3 bar Item4 - 1 foo, 2 bar Item1 - 2 foo, 2 bar Item2 - 0 foo, 1 bar
Hopefully, that will clarify what the code does and also allow you to modify the regex to suit your data.
You can delete the extra map block once you've seen what the sprintf is doing, but I would leave the die...unless code in place so that you will detect and get a record of any malformed records in use.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: What is sprintf doing?
by McMahon (Chaplain) on May 25, 2004 at 18:10 UTC | |
by BrowserUk (Patriarch) on May 25, 2004 at 18:28 UTC | |
by McMahon (Chaplain) on May 25, 2004 at 18:46 UTC |