Moreno, Javier wrote:
> Please help,
> 
> I have the following code:
> 
> # NAME: lexnum
> # Schwartzian Transform algorithm. Returns an array with the
> sorted keys
> in lexical and
> # numeric ascending order.
> # ARGUMENTS: hash_reference
> 
> sub lexnum {
> 
>       my ($hashref) = @_;
> 
>       return map { $_->[0] }
>               sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2] ||
> $a->[3] cmp $b->[3] || $a->[4] <=> $b->[4] }
>               map  { [$_,&Transform()] }
>               keys %{$hashref};
> }
> 
> # NAME: Transform
> # Part of the Schwartzian Transform algorithm. Splits out the
> values so
> correct sorting on different types
> # of key values. Returns the values to the map function # ARGUMENTS:
> NO VALUES 
> 
> sub Transform {
> 
>       if ($_ =~ /^(\D+)(\d+)(\D+)(\d+)$/) {
>               return ($1,$2,$3,$4);
>       }
>       if ($_ =~ /^(\D+)(\d+)(.*)$/) {
>               &Output("<p>Returning:($1,$2)</p>\n");
>               return ($1,$2);
>       }
>       if ($_ =~ /^(\D+)$/) {
>               return ($1);
>       }
> }
> 
> The little &Output function is indeed telling me the if
> worked fine and
> separated the values (an alpha character an a numeric value, like A1,
> A2, A3, etc). However when lexnum tries to return something,
> it returns
> nothing:
> 
> my ($hashref) = \%::hash;
> 
> &Output("<p>Reference to hash: $hashref</p>\n");
> 
> my ($sorted) = lexnum ($hashref);
> 
> &Output("<p>Returning value:$sorted</p>\n");
> 
> The $sorted value comes up with nothing. It should return a reference
> to the new sorted hash.

Not so. Your own documentation for lexnum says that it returns an array
of sorted keys, not a hash ref.

Also, consider that your Transform function doesn't necessarily return
all of the values that the sort function expects, some or all of them
may be undefined. This will lead to warnings like "Use of uninitialized
value..." if warnings are enabled (as they should be, of course).

Also on the subject of Transform, it should not be called with the '&'
prefix, and it would probably be safer to pass the string to Transform
as an argument rather than relying on $_.

HTH

-- 
Brian Raven
 


-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely 
for the intended addressee(s). Unauthorised reproduction, disclosure, 
modification, and/or distribution of this email may be unlawful. If you 
have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message 
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary 
companies.
-----------------------------------------------------------------------


_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to