Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Possible pitfall with Slices

by fundflow (Chaplain)
on Nov 07, 2000 at 00:56 UTC ( #40239=perlquestion: print w/replies, xml ) Need Help??

fundflow has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: Possible pitfall with Slices
by Fastolfe (Vicar) on Nov 07, 2000 at 00:59 UTC
    A better way of doing this:
    @a{qw{ a b c }} = (); # not (1)x3 or whatever foreach (qw{ a b c }) { print "$_\n" if exists $a{$_}; }
    Thus we don't have to worry about being sure the values of the hash are all present and what-not. Specify an empty list on the right-hand-side to set the values all to undef, and just use exists to see if it's there. It's also slightly faster (but not much).
(tye)Re: Possible pitfall with Slices
by tye (Sage) on Nov 07, 2000 at 03:19 UTC

    I can see points on both sides here. I like a middle ground of a warning for assigning a scalar to a list of more than one element. So no behavior would be changed but under "-w", the following would solicit a warning:

    ( $x, $y )= $z; @array[1,2]= $z; @hash{qw(a,b)}= $z; @hash{@array}= $z x @array; # unless 1==@array
    but the following would not:
    ( $x )= $z; ( $x, $y )= ( $z ); @array= $z; # sorry, I think this has to remain unwarned @array[1]= $z; # but already has its own warning @array[1,2]= ( $z ); @hash{qw(a,b)}= ( $z ); @hash{@array}= ( $z ) x @array;
    Note how this would catch the mistake a senior monk made in this very thread.

            - tye (but my friends call me "Tye")
Re: Possible pitfall with Slices
by fundflow (Chaplain) on Nov 07, 2000 at 01:06 UTC
    Thanks for the info, note that the subject is not speed but correctnes.
      I'm assuming this was in reply to my posting.. I was just offering it as a better way of doing what you were trying to do. Building a hash and setting all of its values to 1 just so you can use that 1 as a true value in a test for the presence of a key just screams exists at me. :)

      And, following from that logic, if you're going to use exists, what's the point in worrying if you have all of your hash values set to 1 (or anything at all)? Just put an empty list and don't worry about it, and all is well.

      Good luck.

        Your posts give partial answer. While it is possible to use exist() in my example above, it is not always what you want. What if the actual value is important?
        @employess=( 'This guy', 'That guy', 'Fastofle'); @base_salary{@employees}= 100000; or @netmask{@mymachines}="";

        In any case, some people (like me) might fall in to this trap and the subject of my post was to warn them.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://40239]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2021-04-19 07:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found