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

Re: Possible pitfall with Slices

by fundflow (Chaplain)
on Nov 07, 2000 at 01:06 UTC ( #40242=note: print w/replies, xml ) Need Help??

in reply to Possible pitfall with Slices

Thanks for the info, note that the subject is not speed but correctnes.

Replies are listed 'Best First'.
RE: Re: Possible pitfall with Slices
by Fastolfe (Vicar) on Nov 07, 2000 at 01:08 UTC
    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.

        OK, it is a potential pitfall, but why would you expect a single value (or list of size 1) to get stretched automatically as big as the size of the list you're using in the hash slice? Better to do something like:

        @netmask{@mymachines} = "" x scalar @mymachines;

        Which makes your intent as obvious as it could be.

        Update Fastolfe reminds me that the above code won't do what I want it to. (it would generate a scalar that consists of the size of @mymachines iterations of "" -- list context confuses me yet again! =) To get a list as long as @mymachines, you need to do

        @netmask{@mymachines} = ("") x scalar @mymachines;

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        The code I posted was relevant only to the example you gave. Sorry if that wasn't clear. I did not intend to suggest that exists is the best thing to use in all circumstances. If the values of your hash are important, by all means inspect them. If you're just filling a hash to keep track of the presence of certain items (keys), however, it's a little cleaner to simply see if that key exists in the hash, not to try and test for a true value at $hash{$key}.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://40242]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2018-06-20 15:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.