You might have seen a hash slice. If you have an array of names that look like:

my @fields = qw( name state age );

then you could assign all the corresponding fields in the %identity hash with the following assignment:

@identity{ @fields } = qw( billy colorado 21 );

Even without the array, it's still a nice technique for making things line up:

@identity{ qw/ name state age / } = qw/ billy colorado 21 /;

I only use that sort of construct if I'm modifying the existing keys of a hash. When creating a hash, I'd probably make a list with the fat comma:

my %identity = ( name => 'billy', state => 'colorado', age => 21, );

The trailing comma in the list is fine. This is a syntactic nicety that helps you add and remove list pairs in the code without have to worry about getting the last line right (which is probably the most frequent error I make when composing SQL statements).

Note that the fat comma list is not necessary, as shown by davido. Nonetheless, I would be inclined to use whitespace to help the eye line up the pairs.

my %identity = qw/ name billy state colorado age 21 /;

update: corrected the hash slice syntax to use curly braces instead of parentheses. /me slaps head.


In reply to Re: Multiple variables & values in one line by grinder
in thread Multiple variables & values in one line by einerwitzen

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.