Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Possible pitfall with Slices

by fundflow (Chaplain)
on Nov 07, 2000 at 01:06 UTC ( [id://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?
      e.g.
      @employess=( 'This guy', 'That guy', 'Fastofle'); @base_salary{@employees}= 100000; or @netmask{@mymachines}="255.255.0.0";

      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} = "255.255.0.0" 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 "255.255.0.0" -- list context confuses me yet again! =) To get a list as long as @mymachines, you need to do

        @netmask{@mymachines} = ("255.255.0.0") 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://40242]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-24 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found