Hello Perl Monks

I am a little perplexed on the expected output that seems to be effecting me. I have attached a code smippit.

When the script runs, menu option of 16 items will display. If a selection is made between 1-16, then my "print Array count #2" line displays as expected..16

However, if a selection is made of >16, then print Array count #2" is not displaying correctly. It seems to display the value of $ANS or somehow possibly modifying the array????....but I am not sure.

Just wondering if you can look at it and see where I am going wrong or how it needs to be fixed/addressed

use strict; use warnings; system('cls'); my $SWOS=10; Menu_Main(); sub Menu_Main { my @menu=("","5x & 8x Series", "5x Series", "8x Series","Other"); my @arr = (); my $arrcnt=0; my $MMANS; my $menu=1; my $ANS; #$arr = Text to display, Menu position/selection#, Menu Section, +Numerical Reference $arr[$arrcnt][0] = "First", $arr[$arrcnt][1] = 8, $arr[ +$arrcnt][2] = 1, $arr[$arrcnt][3] = 27, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "5x First", $arr[$arrcnt][1] = 13, $arr[ +$arrcnt][2] = 2, $arr[$arrcnt][3] = 28, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "Second", $arr[$arrcnt][1] = 9, $arr[$ar +rcnt][2] = 1, $arr[$arrcnt][3] = 29, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "Third", $arr[$arrcnt][1] = 10, $arr +[$arrcnt][2] = 1, $arr[$arrcnt][3] = 30, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "8x- Fourth", $arr[$arrcnt][1] = 21, $arr[$a +rrcnt][2] = 3, $arr[$arrcnt][3] = 31, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "5x- Fifth", $arr[$arrcnt][1] = 17, $arr +[$arrcnt][2] = 2, $arr[$arrcnt][3] = 32, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "8x- First", $arr[$arrcnt][1] = 18, $arr +[$arrcnt][2] = 3, $arr[$arrcnt][3] = 33, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "8x- Second", $arr[$arrcnt][1] = 19, $arr[$a +rrcnt][2] = 3, $arr[$arrcnt][3] = 34, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "5x- Fourth", $arr[$arrcnt][1] = 16, $arr[$a +rrcnt][2] = 2, $arr[$arrcnt][3] = 35, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "8x- Third", $arr[$arrcnt][1] = 20, $arr +[$arrcnt][2] = 3, $arr[$arrcnt][3] = 36, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "5x- Second", $arr[$arrcnt][1] = 14, $arr[$a +rrcnt][2] = 2, $arr[$arrcnt][3] = 37, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "XXX- First", $arr[$arrcnt][1] = 23, $arr[$a +rrcnt][2] = 4, $arr[$arrcnt][3] = 38, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "Fouth", $arr[$arrcnt][1] = 11, $arr +[$arrcnt][2] = 1, $arr[$arrcnt][3] = 39, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "Fifth", $arr[$arrcnt][1] = 12, $arr +[$arrcnt][2] = 1, $arr[$arrcnt][3] = 40, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "8x- Fifth", $arr[$arrcnt][1] = 22, $arr +[$arrcnt][2] = 3, $arr[$arrcnt][3] = 41, $arrcnt++, if ($SWOS ==10); $arr[$arrcnt][0] = "5x- Third", $arr[$arrcnt][1] = 15, $arr +[$arrcnt][2] = 2, $arr[$arrcnt][3] = 42, $arrcnt++, if ($SWOS ==10); + @arr = sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] } @arr; print "Select [1-" . ($#arr+1) . "].\n"; print "\n$menu[$menu]\n"; for (my $i=0; $i<=$#arr; $i++) { if ($arr[$i][2] != $menu) { print"\t\t-------------------------------------------------- +--------\n"; print "$menu[$arr[$i][2]]\n"; $menu=$arr[$i][2]; } #print "\t\t$arr[$i][1]. $arr[$i][0]\n"; print "\t\t" . ($i+1) . ". " . $arr[$i][0] . "\n"; } print "Selection ==> "; $ANS=<STDIN>; chomp $ANS; print "Array count #1: " . ($#arr+1) . " \n"; # Print elements in +array print "Processing ANS: $ANS\n"; $MMANS=$arr[($ANS-1)][3]; print "Array count #2: " . ($#arr+1) . " \n"; # Print Elements in +array again print "MMANS is: $MMANS\n"; }
Within Menu Range Selection ==> 8 Array count #1: 16 Processing ANS: 8 Array count #2: 16 MMANS is: 42 Outside of Menu Range Selection ==> 30 Array count #1: 16 Processing ANS: 30 Array count #2: 30 Use of uninitialized value $MMANS in concatenation (.) or string at C: +\temp\array.pl line 57, <STDIN> line 1. MMANS is:

In reply to Count of element in array - Help by g_speran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.