Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Debug a sort block

by rpaskudniak (Novice)
on Jan 31, 2020 at 19:53 UTC ( [id://11112202]=perlquestion: print w/replies, xml ) Need Help??

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

Perl revision 5 version 16 subversion 3

Greetings, oh Great Ones!

Here is a sort command that is barfing on me:

my @ptn_sortd = sort { ${$partition_h{a}}{full_tabname} cmp ${$partition_h{b}}{full_tabname}} @parnums;
This is greeted with a barrage of this error message:
Use of uninitialized value in string comparison (cmp) at ./<script nam +e> line 156. at ./<script name> line 156. Use of uninitialized value in string comparison (cmp) at ./<script nam +e> line 156. at ./<script name>l line 156. Use of uninitialized value in string comparison (cmp) at ./<script nam +e> line 156. at ./<script name> line 156. ....

I have already proven that no item in the hash has an undefined {full_tabname} member. I used

grep {defined(${$partition_h{$_}}{full_tabname}) }

to weed out the one such holdout. (That undefined name is separate mystery, out of scope for this question.) I have also confirmed that when I supply an actual value, the syntax does indeed return a string.

By the way, the hash keys are large-ish integers.

What could I possibly be doing wrong to deserve this frustrating punishment?

Thanks much for help here! -- RP

Replies are listed 'Best First'.
Re: Debug a sort block
by AnomalousMonk (Archbishop) on Jan 31, 2020 at 20:05 UTC
    ${$partition_h{a}}{full_tabname}

    Do you really mean  ${$partition_h{a}}{full_tabname} and not  ${$partition_h{$a}}{full_tabname} (a vice $a)?

    Update: It is possible (though usually very tedious) to put debug statements into a sort block as long as the comparison is the last expression evaluated:

    c:\@Work\Perl\monks>perl -wMstrict -e "use Data::Dump qw(dd); ;; my @sorted = sort { dd $a, $b; $a cmp $b; } (qw(d a c e b), undef); dd \@sorted; " ("d", "a") ("c", "e") ("b", undef) Use of uninitialized value in string comparison (cmp) at -e line 1. ("a", "c") ("c", "d") ("d", "e") ("a", undef) Use of uninitialized value in string comparison (cmp) at -e line 1. ("a", "b") ("c", "b") [undef, "a", "b", "c", "d", "e"]
    (Update: Changed example code to use Data::Dump::dd() instead of print and to include an undef element in the list to be sorted.)


    Give a man a fish:  <%-{-{-{-<

      > Update: It is possible (though usually very tedious) to put debug statements into a sort block

      In this case I'd rather replace the sort block with a function, it's match easier to set breakpoints and watch expressions then.

      DB<1> sub by_num { return $a <=> $b } DB<2> x [sort by_num 1,11,2,12] 0 ARRAY(0x334b888) 0 1 1 2 2 11 3 12 DB<3>

      update

      added return to make mechanism obvious.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Debug a sort block
by LanX (Saint) on Jan 31, 2020 at 21:22 UTC
    It should be $a and $b not a and b °

    On a side note:

    > ${$partition_h{$a}}{full_tabname}

    your dereferencing syntax is kind of weird, the following is much easier to read and does the same:

     $partition_h{$a}{full_tabname}

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    °) oh AnomalousMonk already told you! :)

Re: Debug a sort block
by karlgoethebier (Abbot) on Feb 01, 2020 at 14:37 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11112202]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found