Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

perl: remove elements of array2 from array1

Name: Anonymous 2010-05-14 6:41

Hi, I'm learning perl programming and would appreciate some help. How do I write code to remove elements of array2 from array1

I.e.

@array1={'a','b','c','d'}
@array2={''a','z','c','x'}


(magic happens, processes @array1 and @array2)

dump @array1 gives 'b','d'

Any clues?

Thanks!

Name: Anonymous 2010-05-14 6:45

nevermind, found something

@array1=(1,2,4,6,7,8);
@array2=(1,2,3,5,6,7);

for $i (@array1) {
   print "$i\n" if ! grep {$i == $_} @array2;
}

Name: Anonymous 2010-05-14 8:17

>>1
I have discovered your problem:
I'm learning perl programming

Name: Anonymous 2010-05-14 9:48

>>3
Expert in depth analysis.

Name: Anonymous 2010-05-14 13:21

>>> array1 = {'a', 'b', 'c', 'd'}
>>> array2 = {'a', 'z', 'c', 'x'}
>>> array1 -= array2
>>> array1
{'b', 'd'}

Name: Anonymous 2010-05-14 13:33

>>5
Wait, you can do that?  What's the big-Oh for that?

Name: Anonymous 2010-05-14 13:36

>>6
girlsAll-GirlsWithBoyFriends

Name: Anonymous 2010-05-14 13:49

>>6
It's constant time, like all interpreted languages.

Name: Anonymous 2010-05-14 15:45

>>6
O(n) for sets, obviously.

Name: Anonymous 2010-05-14 15:55

>>9
Did you mean O(n2)?

Name: Anonymous 2010-05-14 16:32

Name: Anonymous 2010-05-14 16:34

>>10
Depends on how you implement sets. I'm no computer scientist, but it seems to me that you could get it in O(n) if you used sorted lists for your set implementation.

Name: Anonymous 2010-05-14 16:43

>>12
Python's sets are just dicts with null values, I think.

Name: Anonymous 2010-05-14 17:13

>>13
I believe so, but I only remembered hashtables after I'd written my post.

Name: Anonymous 2011-02-03 6:24

Don't change these.
Name: Email:
Entire Thread Thread List