Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re^3: Homework question list

by Tanktalus (Canon)
on May 22, 2005 at 15:37 UTC ( #459353=note: print w/replies, xml ) Need Help??

in reply to Re^2: Homework question list
in thread Homework question list

Your example fails if @a1 or @a2 have a value more than once. For example, @a1 = (1, 2, 3, 2, 1) and @a2 = (4, 5, 6, 5, 4). Your grep would show 1, 2, 4, and 5 as elements existing in both arrays, when the right answer is obviously zero.

I will also disagree with cLive ;-) that "really smart people" use the undef'd hash slices to save memory - if memory is a concern, sure. But in general, the difference is not going to be significant. Premature optimisation and all that. I've even found sometimes where using the standard "++$hash{$value}" turns out to be handy three months later when the number of times a value shows up becomes relevant. I didn't need to change nearly as much code because I wasn't "really smart" according to cLive's definition.

Anyway, as always, TIMTOWTDI, so being able to use the undef'd hash slice is still a tool to keep handy:

my %a1; undef @a1{@a1}; my @in_both = grep { exists $a1{$_} } @a2;
Unfortunately, this has the side effect of showing duplicates if @a1 has a value, and @a2 has that value multiple times. Which, of course, may be what you want, but it's not explicit in the original requirement.
my (%a1, %a2); undef @a1{@a1}; undef @a2{@a2}; my @in_both = grep { exists $a1{$_} } keys %a2;
Oh, and I also recommend better variable names than what I'm using here. :-)

Update: Added the italicised part in the last line.

Replies are listed 'Best First'.
Re^4: Homework question list
by Aragorn (Curate) on May 22, 2005 at 20:09 UTC
    The example isn't a real good one, I know. I was just playing around with variations on a theme, not trying to get a bullet-proof solution. But still, good point about the example :-)

    cLive ;-)'s solution is a variation on a theme explained in the Cookbook. I was under the impression that there existed some really nifty hash slice trick to get the results that I didn't know about, hence the question.

    In my production code, I use good variable names. When experimenting, I'm not as picky :-) I probably should be when posting code here.

    Thanks, Arjen

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://459353]
[Your Mother]: The good news is, they think they are influencing people but Americans at least are irretrievably biased. All that can be accomplished is a little spike in domestic violence. :P
[LanX]: YM: watch your mouth or I'll show up with my automatic gun at your nursery ...
[LanX]: ;-P
[LanX]: What I learned now is to hide my friends list at FB (not that I have many ...)
[Your Mother]: Careful. They're listening!
[LanX]: I mean hiding my friends FROM my friends
[Your Mother]: Yeah. I don't know what they expect. If a stranger can see the post or list or whatever, so can a harvesting program.
[Your Mother]: HA.
[NodeReaper]: he's already hiding from me :(
[Corion]: Your Mother: Even I don't remember what "related and neat" I could have that would fit to 1211240 :) Maybe "pjax", which turns a static site into a Javascript-partial -reloading site? That's purely Javascript though

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2018-03-19 13:31 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.