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

Re^2: Syntax explanation required (uniq)

by LanX (Chancellor)
on Jan 03, 2013 at 01:58 UTC ( #1011382=note: print w/replies, xml ) Need Help??


in reply to Re: Syntax explanation required
in thread Syntax explanation required

I was meditating about advising List::MoreUtils:uniq as a best practice, cause I thought it prevents any stringification problems.

Unfortunately it doesn't! :(

DB<125> $a=["A"] => ["A"] DB<126> $b=["B"] => ["B"] DB<127> @a=($a,"$a",$b,"$b") => (["A"], "ARRAY(0x96cb0f0)", ["B"], "ARRAY(0x96cfd80)") DB<128> @b= uniq @a => (["A"], ["B"])

thats quite disappointing for an XS-module ...

EDIT: Well at least it preserves order...

Cheers Rolf

) I should explain, the problem with using hashes for set operations is that keys are stringified, that means working with anything which isn't a string or at least a scalar comes with a risk.

Practically it's not possible to safely use a ref as a key because of the resulting string is irreversible.

DB<130> $h{$a}=5 => 5 DB<131> %h => ("ARRAY(0x96d1f00)", 5)

UPDATE:

improved code example...

Replies are listed 'Best First'.
Re^3: Syntax explanation required (uniq)
by ghosh123 (Monk) on Jan 03, 2013 at 12:50 UTC
    Thank you all. Very helpful !!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1011382]
help
Chatterbox?
[Lady_Aleena]: shmem, I already have the plugin installed, just not active.
[shmem]: if you have 1 optional argument, place that at the end of the list. If you have more, go for named parameters.
[Lady_Aleena]: 1 manditory, 2 optional.
[Discipulus]: my ($need,$opta,$optb ) = @_; .. if $opta..
[shmem]: all else leads to trouble, even if the third argument depends on the existence of the second. That may become brittle.
[Discipulus]: but if have case like subname(15,undef,3 ) maybe bettere named parameters
[Lady_Aleena]: I don't want to have to do: alpha_menu($hash, undef, $type);
[Lady_Aleena]: Or what Discipulus said.
[shmem]: Lady_Aleena: geany supports ctags.
[Discipulus]: a good compromise can be my ($need, $opts_ref) = @_ a scalar and an hash reference

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2017-04-27 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (506 votes). Check out past polls.