If you want to keep your names in the same order, since thay already seem to be sorted, then you can't depend on
keys giving you back the names in sorted order. Adding a few more names to your list gives unpredictable orders. Thus you need to sort them to preserve your original order. Doing
use strict;
use warnings;
our @lines = <DATA>;
chomp @lines;
our %uniques;
@uniques{@lines} = ();
print "$_\n" for keys %uniques;
__END__
Name1
Name2
Name2
Name3
Name3
Name3
Name3
Name4
Name4
Name5
Name5
Name5
Name5
Name5
Name6
Name6
Name7
Name7
Name7
Name8
Name8
Name9
Name9
Name9
Gives
Name8
Name9
Name1
Name2
Name3
Name4
Name5
Name6
Name7
Adding a sort to the print ... line like this
print "$_\n" for sort keys %uniques;
corrects the problem (assuming your "names" are real names that sort lexically, as going on to Name10, Name11 etc sort after Name1 and before Name2).
Cheers,
JohnGG
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.