Re^8: Sorting text-number values
by merrymonk (Hermit) on Nov 30, 2016 at 11:21 UTC
|
Fair enough - but I need to use this sort in a Perl application that may need facilities in 5.20 which are not in 5.010. To some extent this will be fine unless the difference in behaviour is 'hidden' so that it is not simple to detect 'odd' things that are going wrong.
Also, I do need to understand where the data is stored or how to store the sorted data in an array. | [reply] |
|
|
Also, I do need to understand where the data is stored or how to store the sorted data in an array.
Just assign to an array!
my @sorted =
map { substr $_, 54 }
sort
map { do {
no warnings qw{ uninitialized };
pack qq{A${width}NA*}, m{(.*\D)?(\d+)$}, $_;
}
}
@data;
Simples!
| [reply] [d/l] |
|
|
Sadly I have just realised that this solution gives multiple entries for data which has values which are the same.
I appreciate it would be simple to go through the sorted array and removed any duplicates.
However, can this also be done with a modification to the 'pack' statement or my some other means?
| [reply] |
|
|
|
|
|
|
I thought it might be, it was just finding it out which was the problem!
| [reply] |
|
|
Fair enough - but I need to use this sort in a Perl application that may need facilities in 5.20 which are not in 5.010.
And in what way do you suppose having the use 5.010; statement in the code would prohibit that?
You cannot take working code, remove lines which you don't understand and then complain that it no longer works. This is as true of Perl as in any other language.
If you do remove a line from working code which then breaks then the appropriate course of action is to learn what that line means. In many cases this is as simple as running perldoc -f use which would have told you. Among other very useful information it says:
"use VERSION" also lexically enables all features available in the
requested version as defined by the "feature" pragma, disabling
any features not in the requested version's feature bundle. See
feature. Similarly, if the specified Perl version is greater than
or equal to 5.12.0, strictures are enabled lexically as with "use
strict". Any explicit use of "use strict" or "no strict" overrides
"use VERSION", even if it comes before it. Later use of "use
VERSION" will override all behavior of a previous "use VERSION",
possibly removing the "strict" and "feature" added by "use
VERSION". "use VERSION" does not load the feature.pm or strict.pm
files.
... etc. Feel free to play with code, take it apart, break it, reassemble it. All this is good for learning. But when it breaks, try to understand how and why.
HTH, Hippo
| [reply] [d/l] |
|
|
| [reply] |
|
|
|
|
use 5.010;
This enables features from Perl 5.10, but works on any version of Perl later than that.
As Perl 5.20 is a later version, use 5.010; has little effect there beyond enabling the say keyword. | [reply] [d/l] [select] |
|
|
Thanks for that and all your other contributions this morning. It is not quite what I thought.
However, johngg's pointer to storing the sorted data means that say is no longer required and the problem goes away.
I think my overall problem was that I do not understand what 'say' does. So far I have failed to find an explanation but I will continue my search!
| [reply] |
|
|