Hello, I hope you can help me. I'm having a problem with sort dropping the last element of an array I need to print. The array is part of a hash, and I just need to sort the elements of the array and print them out. Unfortunately when I go to sort and print, the last element disappears. I know the last element is disappearing because I've gone and added code to tell me how many elements are in the array, and then to print them first by accessing each individual element, without sorting. What's weird is that when I sort the array, it comes back as the same size as the unsorted, but it doesn't print the last line. Here's the code, followed by an output of the script.
foreach $test (keys %Tests) { print "Test and students are\n $test:\n"; $" = "\n"; # Only do the sort and join with newlines # if there's more than one student. if (@{ $Tests{$test} } > 1){ @tempvar1 = sort( @{ $Tests{$test} }); $i = 0; #Making sure everything is sorted, and will #actually print. Had problems with lines being #dropped. $tempvar = @{ $Tests{$test} }; print $tempvar, "\n"; while ($i < @{ $Tests{$test} }){ print $Tests{$test}[$i]; print "\n"; $i++; } $tempvar2 = @tempvar1; print $tempvar2, "\n"; print "@tempvar1"; # print $tempvar1 . "hello\n"; # print join("|", sort @{ $Tests{$test} }); } else {print "@{ $Tests{$test} }\n";} print ",,,,,,,,,\n,,,,,,,,,\n"; }
Output:

Test and students are
Development:
4
"Drayton, xxx",,3 Years and 7 Months,,,,,,
"Areen, xxx",,3 Years and 9 Months,,,,,,
"Lyles, xxx",,4 Years and 0 Months,,,,,,
"Coleman, xxx",,3 Years and 9 Months,,,,,,
4
"Areen, xxx",,3 Years and 9 Months,,,,,,
"Coleman, xxx",,3 Years and 9 Months,,,,,,
"Drayton, xxx",,3 Years and 7 Months,,,,,,


In reply to print dropping a line? by Anonymous Monk

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.