I have some code that’s working, but spitting out a lot of warnings.
foreach my $item ( sort {$item_rank{$a} <=> $item_rank{$b}} @items{
...
}
My problem is that not every item has a rank, and so my output is littered with warnings. I want to make the items with no rank last.
I am thinking of changing the code to as follows:
foreach my $item ( sort {
$item_rank{$a} = 99999 if(!exist $item_rank{$a});
$item_rank{$b} = 99999 if(!exist $item_rank{$b});
$item_rank{$a} <=> $item_rank{$b}} @items{
...
}
My question is, is there a particular value that I can set it to instead of 99999, although I will never reach 99999 in my current setup, I want my code more robust.
Thanks
You can do it one of 2 ways:
If you have defined max ceiling, default to it (To simplify the code, I am assuming 0 is not a valid rank)
Or, to avoid assigning to the hash:
Better yet, check for undefs explicitly in your expression (remember that sort’s code block can be ANY expression, which returns negative, 0 or positive ala “
<=>“: