in reply to Re: Hash Manipulation
in thread Hash Manipulation

Thank you for your insight. You have solved some of my issues and moved me along. I'm getting the data in the format I would like it. I am able to print the screens with the correct number of buttons in the "\multirow{??}".
The only issue I have now that is keeping me from completing this is, counting down buttons available per page and then formatting the changes to "\multirow{??}" as it is effected by being split between pages.
Sorry about the order of the sorted elements, I wasn't thinking about it when I was typing up the sample result. I was trying to make sure that a $key was page broken.

This is what I have now. I think that if I manipulate a few more if statements I can count down remaining buttons and check that each time that $ele = 0, to determine if I need to split it and update the "\multirow{??}". I may need to count down buttons per screen remaining also.

my $MaxButtons = 5; my $RemainingButtons = $MaxButtons; my $ButtonsOnPage = 0; for $key (sort keys %HashCount) { for $ele (sort keys %{$HashCount{$key}}) { if ($ele eq 0){ } else { if ($ele eq 1){ print "\\multirow{$HashCount{$key}->{0}}{0.85in}{\\myt +abhead{$key}} \& " . $HashCount{$key}->{$ele} . "\n"; } else { print " \& " . $HashCount{$key}->{$ele} . "\n"; } } } }

Replies are listed 'Best First'.
Re^3: Hash Manipulation
by hippo (Archbishop) on Jun 08, 2021 at 15:19 UTC

    In your code you have this block (I've shortened the print statements for clarity):

    if ($ele eq 0){ } else { if ($ele eq 1){ print "Condition: ele is 1\n"; } else { print "Condition: ele is neither 1 nor 0\n"; } }

    This is confusing because of the empty block straddling the first 2 lines. There are a couple of ways to avoid it in general. One is to negate the condition:

    if ($ele ne 0){ if ($ele eq 1){ print "Condition: ele is 1\n"; } else { print "Condition: ele is neither 1 nor 0\n"; } }

    The other is to turn the if into an unless:

    unless ($ele eq 0){ if ($ele eq 1){ print "Condition: ele is 1\n"; } else { print "Condition: ele is neither 1 nor 0\n"; } }

    Either of these removes the empty block, so that's a plus. But we can go farther. The logic can be simplified because really you have only 2 conditions as indicated by my rewritten print statements. So we can remove the outer condition by folding it into the else like so:

    if ($ele eq 1){ print "Condition: ele is 1\n"; } elsif ($ele ne 0) { print "Condition: ele is neither 1 nor 0\n"; }

    Finally, deducing from your previous code that $ele is only ever a whole number you can switch to using numerical comparisons rather than string comparisons (== instead of eq, etc.). Perhaps this is the clearest:

    if ($ele == 1) { print "Condition: ele is 1\n"; } elsif ($ele > 1) { print "Condition: ele is neither 1 nor 0\n"; }

    I understand that you are just hacking about with this code but wanted to point out these alternative ways of going about the logical if-blocks. The simpler you can make it the fewer bugs can creep in and that has to be a good thing.


    🦛