first, i'll explain my code:
my %hash = map { split } @names_marks;
okay, let's break it down.

 @names_marks is a list of items, containing strings with two items seperated by a single space, such as 'keith 90'. you want to seperate these items.

split, by default, will split on spaces, and will split $_. it's in the documentation: online at split, or command line at perldoc -f split. so split could be replaced with split ' ', $_

i can be sure that split will return a list, since the doc (for perl 5.6.1) says scalar context is deprecated. since i know the data only contains one space, an item on either side, i can use these two items to assign to a hash.

it's perfectly legal to say

my %hash = ( 'bob', 1 ); # or my %hash = ( bob => 1 );
where i'm assigning the first list element as a key in %hash, and the second list element as the key's value.

on a sidenote--you can use Data::Dumper; to see into data structures. add <use Data::Dumper;</code> to the top of the script, and print Dumper [%hash]; after the hash is created. you'll see the keys and values. this is particularly helpful for debugging complex data structures.

map is somewhat similar to for or foreach, when used to iterate over a list, see the doc: map. (by the way, for and foreach are the same, docs: for, foreach.)

i could turn my map into a for:

my %hash; for( @names_marks ) { %hash = split } # or, expanding split for( @names_marks ) { %hash = split ' ', $_ }
note there's no semi-colon between the braces--this is okay because the final statement in braces does not require a trailing semi-colon.

okay, i hope that helps you a bit. now, on to the problem of two persons with the same name. my questions to you are: how do *you* know who's who? if you're reporting who got what marks, and there are two johns, wouldn't each want to know which mark he received?

in this case, you'll need unique names, like 'john1', or 'john q. public'. the former will work with my current implementation. the latter will require some coding changes. for instance, space is no longer a good seperator for @names_marks, because the names might have spaces in them. instead, use a pipe, or colon, or carat, or question mark, or exclamation point. make sure it's some character that won't show up in either data field.

then, split the data on the seperator instead of space. hash keys can have spaces in them, so the rest of my example should still work. although, if this were important code, i'd recommend a more robust implementation than the one i've provided.

~Particle ;Þ


In reply to Re: Re: Re: sort hash elements... by particle
in thread sort hash elements... by kiat

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.