Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Assist in understanding old code

by ozgurp (Beadle)
on Feb 28, 2005 at 22:48 UTC ( #435219=perlquestion: print w/replies, xml ) Need Help??

ozgurp has asked for the wisdom of the Perl Monks concerning the following question:

Hi All! I have been given a task of modifying someone else's program. The program i have to modify does not use strict and warnings. So I decided to put them in, and I am having all sorts of error messages. Anyway I fixed most of these errors, but there is a a bit that I just cannot fix and also I don't understand the syntax. Could you help me figure out what the program is trying to do? Here is the code (@ldx array is a multidimensional array and I cannot put it here because it is quite big):
my $ct=0; my @nxxult = (); my @Ultmnsort = (); my @indexUlt = (); for(my $rn=4;$rn<=32;$rn +=4) { for(my $bn=1;$bn<=$row[0];$bn++) { $nxxult[$bn]=$ldx[$bn][$rn]; } #@indexUlt=sort{$nxxult[$a]<=>$nxxult[$b]} 0 ... $#nxxult; $Ultmnsort[$ct]=$indexUlt[0]; $Ultmxsort[$ct]=$indexUlt[$#indexUlt]; $ct++; [$#indexUlt]\n"); print TEST3 ("ct = $ct\n"); }
This is the syntax I don't understand (I now it is doing some kind of sorting but this is where the program is giving error messages):
#@indexUlt=sort{$nxxult[$a]<=>$nxxult[$b]} 0 ... $#nxxult;
Here is the error message:
Use of uninitialized value in numeric comparison (<=>) at C:\ l +ine 791, <AREA> line 22371.

Replies are listed 'Best First'.
Re: Assist in understanding old code
by tirwhan (Abbot) on Feb 28, 2005 at 23:52 UTC
    osunderdog has already explained what the code does. The reason you are getting the warning is that the code starts to fill the array at index 1:
    for(my $bn=1;$bn<=$row[0];$bn++)
    but starts to retrieve and sort at index 0
    @indexUlt=sort{$nxxult[$a]<=>$nxxult[$b]} 0 ... $#nxxult;
    Thus, you have an undef value in your comparison at nxxult[0] and you get a warning. Either change the loop to start filling the array at $nxxult[0] or count from 1 ... $#nxxult.
Re: Assist in understanding old code
by osunderdog (Deacon) on Feb 28, 2005 at 23:13 UTC

    if the line wasn't commented out, this line produces an ordered list of array indexes. The indexes are ordered so that the first index in the list is the lowest (ordinal) value from the array (@nxxult).

    For Example:

    use strict; my @nxxult = (3, 5, 29, -1, 398, 585, 920, 45, 39, 281, 03, 591); my @indexUlt=sort{$nxxult[$a]<=>$nxxult[$b]} 0 ... $#nxxult; print join(',', @indexUlt) . "\n"; print "done\n";

    Produces the following output:


    So the item at index 3 is the lowest, item at index 0 is the second lowest, etc.

    "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.

Re: Assist in understanding of old code
by talexb (Chancellor) on Feb 28, 2005 at 22:58 UTC
    #@indexUlt=sort{$nxxult[$a]<=>$nxxult[$b]} 0 ... $#nxxult;

    I would have said that one of the values in the array that's being sorted is undefined, except the whole line appears to be commented out. What's really going on with this code, do you know?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      Sorry it should not have been commented out, so the code is:
      my $ct=0; my @nxxult = (); my @Ultmnsort = (); my @indexUlt = (); for(my $rn=4;$rn<=32;$rn +=4) { for(my $bn=1;$bn<=$row[0];$bn++) { $nxxult[$bn]=$ldx[$bn][$rn]; } @indexUlt=sort{$nxxult[$a]<=>$nxxult[$b]} 0 ... $#nxxult; $Ultmnsort[$ct]=$indexUlt[0]; $Ultmxsort[$ct]=$indexUlt[$#indexUlt]; $ct++; }
      I don't really know what is going on in this code, I am working on it.

        Well then, if the line shouldn't actually be commented out, then the problem is as talexb described, "one of the values in the array that's being sorted is undefined." You have two ways of solving that problem:

        • Use an empty string value instead of undef
        • Turn off the warning

        Which solution you pick will usually depend highly on the application. I tend to structure my apps in a way that an empty string makes more sense than undef, but perhaps this one isn't. In that case, turning off the warning (using a lexically scoped no warnings) would be the best option.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://435219]
Approved by talexb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2022-05-25 13:51 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (90 votes). Check out past polls.