Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

RE: Re: Possible pitfall with Slices

by Fastolfe (Vicar)
on Nov 07, 2000 at 01:08 UTC ( [id://40243]=note: print w/replies, xml ) Need Help??


in reply to Re: Possible pitfall with Slices
in thread Possible pitfall with Slices

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.

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

        Oh man...

        Why?

        Possible answers:

        1. Because it makes sense
        2. Because other languages have it
        3. Because people might want it
        4. Because it could make programs cleaner
        5. Because it could be faster (no need to generate an array)
        6. Because the current way generates a silent bug
        7. Why not?

        Also: Do you know what "255.255.0.0" x scalar @mymachines; returns?
        (hint: not a list)

      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://40243]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found