Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dear Monks,

There is a subtle and quiet way to shoot yourself in the leg when using array slices. It just happened to me and this note is posted with the hope to save others this 'pleasure'.


Array/hash slices are very convinent for vectorizing operations on arrays and hashes. For Matlab users (like me) this is very natural. For example:

@a[1,2,3] = @b[100,200,300];
will copy three of b's elements to a.
Similarly, for hashes:
will put the same elements in the hash a.

<bf>The Pitfall<bf>

It order to fill a hash, one can write the following:

and after that line, the three elements 'a','b','c' will be in %a, BUT they will not contain the value 1!
Thus, the following will not give the expected result:
for (a,b,c,d) { print "$_\n" if $a{$_} } OUTPUT: a
The output contains only 'a' since in the assignment above, $a{a} was assigned the value 1 whereas the rest were created, but are undefined. This is because perl treats the slice as a list and expects the right-hand side to be a list as well, and we only have one element there...

The right way to do it is:

@a{a,b,c}=(1,1,1); or @a{a,b,c}=(1)x3;

The problem here is that this gives a default silent behaviour.

The matlab approach to this, which is very consistent and natural is that assignments like this have to have the same number of elements on both sides. A special case where the r.h.s. is a scalar, is treated as an array of the appropriate size with this repeated element.

It would be nice if when the r.h.s. in the assignment is one element, it will be treated as a list with that element repeated the correct number of times (i.e. Matlab style). If this is too hard to implement, it could be useful to give a warning/error about that.

1. if perl is run with -w, then accessing the elements will warn that the elements are undefined, but this will appear in a different location than the assignment
2. Is this the right place in PM for such node?

In reply to Possible pitfall with Slices by fundflow

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (4)
    As of 2018-01-18 10:24 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (211 votes). Check out past polls.