I took an example from the internet as a start on working with arrays and sort. I am surprised by the different output of
print sort(@myarray);
and
@sarray = sort (@myarray);
print @sarray;
Example
#!/usr/bin/perl
# AN ARRAY
@coins = ("Quarter","Dime","Nickel");
print "@coins";
print "\n\r";
# ADD ELEMENTS
push(@coins, "Penny");
print "@coins";
print "\n\r";
unshift(@coins, "Dollar");
print "@coins";
print "\n\r";
@scoins = sort(@coins);
print sort(@coins);
print "\n\r";
print "@scoins";
print "\n\r";
# REMOVE ELEMENTS
pop(@coins);
print "@coins";
print "\n\r";
# BACK TO HOW IT WAS
shift(@coins);
print "@coins";
print "\n\r";
Output
Quarter Dime Nickel
Quarter Dime Nickel Penny
Dollar Quarter Dime Nickel Penny
DimeDollarNickelPennyQuarter
Dime Dollar Nickel Penny Quarter
Dollar Quarter Dime Nickel
Quarter Dime Nickel
p.s. the code blocks look really small in my preview. Hope this is normal.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.