Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Possible useless use of map

by Marshall (Canon)
on Mar 20, 2010 at 01:52 UTC ( [id://829742]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Possible useless use of map
in thread Possible useless use of map

This idea will certainly work!

I personally try to avoid using undef as an intentional value because:
a) this adds a complication about exists(), defined() later in the code,
b)If you have to export something with an "undef" value to something else other than Perl what do you use? or for that matter even it its Perl, why not 0 or 1 which is easier to parse? undef throws an extra third case into mix. Sometimes this makes sense like SQL's NULL, but why throw that in there if you don't need to?
c) it doesn't save any memory or execution time.

We are getting a bit off topic, but this is a hash slice. To get this work with the =1 idea, I think something like the below is required. There isn't foreach, but there is still a loop (except in the enumerated first case).

I personally like the $hr->{$_} = 1; syntax because it makes clear that we are de-referencing a hash reference.

A short thing about hash slice is at hash slice. My foreach loop can be replaced with this..

#foreach my $num (@$arr) #{ #any one of these does the same thing... #pick one... @$hr{@$arr} = (1,1,1); @$hr{@$arr} = map{1}@$arr; @{$hr}{@$arr} = map{1}@$arr; #}
Oh, the map{1} generates an "@something" that has the same number of "1's" as there are elements in the input @$arr although here it is "anon", having no explicit name. Here that output of the map is used (ie, it is not a "bare" map). In this case, there is still a loop and we've just made the situation more confusing. Clarity of code is important and just because some implementation uses fewer lines doesn't mean that it is faster.

Replies are listed 'Best First'.
Re^4: Possible useless use of map
by Jenda (Abbot) on Mar 20, 2010 at 10:22 UTC
    @$hr{@$arr} = (1) x @$arr;

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

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

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

    No recent polls found