On 8/21/07, Tony Heal <[EMAIL PROTECTED]> wrote:
> OK I added this and I keep getting invalid format
>
> foreach (@newValues){print "$_\n";}
> my @versions;
> while (@newValues)
> {
> chomp;
> die "invalid format" unless
> my ($major, $minor, $build) = /(\d+)(?:-.+)?\.(\d+)-(\d+)/;
> push @versions, [ $major, $minor, $build , $_];
> }
> foreach (@versions){print "$_\n";}
> }
snip
That would be because the code makes no sense. My example read the a
version at a time from the DATA file handle, transformed it, and
pushed it onto an array, then sorted the array and printed it. Yours
has all of the versions in an array and tries to loop over the array
with a while loop (doesn't work to start with) and you never bother to
sort the data. If you aren't reading from a file then you might as
well add the first loop back onto the Schwartzian transform (map ->
sort -> unmap). Please note that
die "bad format" unless
my ($major, $minor, $build) = /(\d+)(?:-.+)?\.(\d+)-(\d+)/;
is one statement and should be indented as above. If you don't indent
it looks like the die and the assignment are unrelated. If you find
the style confusing you may consider using this instead
my ($major, $minor, $build) = /(\d+)(?:-.+)?\.(\d+)-(\d+)/
or die "bad format";
#!/usr/bin/perl
use strict;
use warnings;
#I don't know how you are getting these values
my @newValues = map { chomp; $_ } <DATA>;
print "unsorted\n";
print "$_\n" for @newValues;
@newValues =
#unmap to recover the original data
map { $_->[0] }
#sort
sort {
$a->[1] <=> $b->[1] or
$a->[2] <=> $b->[2] or
$a->[3] <=> $b->[3]
}
#map into a sortable form
map {
die "bad format" unless
my ($major, $minor, $build) =
/(\d+)(?:-.+)?\.(\d+)-(\d+)/;
[$_, $major, $minor, $build]
}
@newValues;
print "sorted\n";
print "$_\n" for @newValues;
__DATA__
16.1-17
16.1-22
16.1-23
16.1-39
16.3-1
16.3-6
16.3-7
16.3-8
16.3-15
16.5-1
16.5-2
16.5-10
16.5-13
15.3-12
15.2-108
14-special.1-2
14-special.1-8
14-special.1-15
14-special.2-40
14-special.2-41
14-special.3-4
14-special.3-7
14-special.3-12
15.2-110
15.2-111
15-special.1-52
15-special.1-53
15-special.1-54
16-special.4-9
16-special.4-10
16-special.5-1
16-special.5-2
16-special.6-6
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/