Re: remove duplicate values from array
On Apr 22, Jon Howe said: Can anyone tell me the best method for removing duplicate array values. I think the FAQ answers this best. perldoc -q uniq -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for Regular Expressions in Perl published by Manning, in 2002 ** stu what does y/// stand for? tenderpuss why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: remove duplicate values from array
Can anyone tell me the best method for removing duplicate array values. eg: @array = qw( a a a b b b c); becomes after some operation I cant seem to figure: @new_array_or_same = (a b c); In this situation, I usually use a hash instead of an array so I can create highly useful statistics of how often each value occurs. So I would do: $hash{a}++ or $hash{b}++ or $hash{c}++ And then, like Jeff said, you could look in the manpage and get your array from this hash you've created using the keys function like so: @array = keys %hash; Cheers, -dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove duplicate values from array
i`m not quite shure because i`m a newbie to, but try using some RegExp like tr/some pattern/final idea/ and dont forget that tr// and s// have many more functions ex. using the (?). The book that i find most explainable about RegExp is Beginning Perl from Wrox Publishing. - Cheers, Miroslav Nikolov - On Mon, 22 Apr 2002, Jon Howe wrote: Can anyone tell me the best method for removing duplicate array values. eg: @array = qw( a a a b b b c); becomes after some operation I cant seem to figure: @new_array_or_same = (a b c); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove duplicate values from array
On Monday, April 22, 2002, at 09:29 , David Gray wrote: Can anyone tell me the best method for removing duplicate array values. eg: @array = qw( a a a b b b c); becomes after some operation I cant seem to figure: @new_array_or_same = (a b c); In this situation, I usually use a hash instead of an array so I can create highly useful statistics of how often each value occurs. good reason to not use an autonomous hash. what I just learned would solve the problem would be my @array = qw( a a a b b b c); my @new_array = keys %{{ map { $_ = 1 } @array}}; since the perldoc -q uniq that jeff provided pokes in that direction - but was 'hash' oriented. and, ok, so I am working out the questions: So why Exactly do I want to have 'map' to begin with? given perldoc -f map ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove duplicate values from array
From: drieux [EMAIL PROTECTED] On Monday, April 22, 2002, at 09:29 , David Gray wrote: Can anyone tell me the best method for removing duplicate array values. eg: @array = qw( a a a b b b c); becomes after some operation I cant seem to figure: @new_array_or_same = (a b c); In this situation, I usually use a hash instead of an array so I can create highly useful statistics of how often each value occurs. good reason to not use an autonomous hash. what I just learned would solve the problem would be my @array = qw( a a a b b b c); my @new_array = keys %{{ map { $_ = 1 } @array}}; since the perldoc -q uniq that jeff provided pokes in that direction - but was 'hash' oriented. Yes you can do that. But it's slow ... my @array = qw( a a a b b b c); my @new_array; { my %h; @h{@array} = (); @new_array = keys %h; } using a named lexical hash and hash slice instead of map is quicker: Benchmark: timing 2 iterations of Faq, Mine... Faq: 10 wallclock secs ( 9.62 usr + 0.00 sys = 9.62 CPU) @ 2078.35/s (n=2) Mine: 1 wallclock secs ( 1.57 usr + 0.00 sys = 1.57 CPU) @ 12722.65/s (n=2) #!perl use Benchmark; @array = ((qw( a a a b b b c d d d f f u u u u u i i a y s e t x)) x 10); sub Mine { my @new_array; { my %h; @h{@array} = (); @new_array = keys %h; } return scalar(@new_array); } sub Faq { my @new_array = keys %{{ map { $_ = 1 } @array}}; return scalar(@new_array); } timethese 2, { Mine = \Mine, Faq = \Faq, }; __END__ and, ok, so I am working out the questions: So why Exactly do I want to have 'map' to begin with? map { $_ = 1 } @array creates a list in form: ( $array[0] = 1, $array[1] = 1, $array[2] = 1, ...) If you then create a hash out of this list, the $array[x] items become keys and the 1s become values. So 1. map { $_ = 1 } @array creates list of keys and values 2. {map { $_ = 1 } @array} creates a reference to an anonymous hash 3. %{{map { $_ = 1 } @array}} dereferences the reference 4. keys %{{map { $_ = 1 } @array}} returns a list of keys. Jenda === [EMAIL PROTECTED] == http://Jenda.Krynicky.cz == There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove duplicate values from array
Hé ! Jenda this slice thing is really very nice. hash{@array1}=array2; I saw it many times used in constructor of Object Oriented Perl's code. Could you explain sequencely the syntax please ? José. - Yahoo! Mail -- Une adresse yahoo.fr gratuite et en français !
RE: remove duplicate values from array
Gosh.. what ever happened to good ol' grep my %seen; @uniq = grep !$seen{$_}++, @array; -Original Message- From: Jon Howe [mailto:[EMAIL PROTECTED]] Sent: Monday, April 22, 2002 12:30 PM To: [EMAIL PROTECTED] Subject: remove duplicate values from array Can anyone tell me the best method for removing duplicate array values. eg: @array = qw( a a a b b b c); becomes after some operation I cant seem to figure: @new_array_or_same = (a b c); The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove duplicate values from array
From: Jos Nyimi [EMAIL PROTECTED] this slice thing is really very nice. @hash{@array1}=@array2; I saw it many times used in constructor of Object Oriented Perl's code. Could you explain sequencely the syntax please ? Well I don't think I can make up a better explanation than the one found in perldoc perldata in section Slices. Jenda P.S.: Nikola? The grep is slower than the slice: Benchmark: timing 2 iterations of Faq, Mine, Nikola... Faq: 10 wallclock secs ( 9.70 usr + 0.00 sys = 9.70 CPU) @ 2061.01/s (n=2) Mine: 1 wallclock secs ( 1.58 usr + 0.00 sys = 1.58 CPU) @ 12642.23/s (n=2) Nikola: 5 wallclock secs ( 4.40 usr + 0.00 sys = 4.40 CPU) @ 4549.59/s (n=2) === [EMAIL PROTECTED] == http://Jenda.Krynicky.cz == There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove duplicate values from array
Ok, thanks ! I read it and every thing becomes clear to me :-) José. Jenda Krynicky [EMAIL PROTECTED] a écrit : From: José Nyimi this slice thing is really very nice. @hash{@array1}=@array2; I saw it many times used in constructor of Object Oriented Perl's code. Could you explain sequencely the syntax please ? Well I don't think I can make up a better explanation than the one found in perldoc perldata in section Slices. Jenda P.S.: Nikola? The grep is slower than the slice: Benchmark: timing 2 iterations of Faq, Mine, Nikola... Faq: 10 wallclock secs ( 9.70 usr + 0.00 sys = 9.70 CPU) @ 2061.01/s (n=2) Mine: 1 wallclock secs ( 1.58 usr + 0.00 sys = 1.58 CPU) @ 12642.23/s (n=2) Nikola: 5 wallclock secs ( 4.40 usr + 0.00 sys = 4.40 CPU) @ 4549.59/s (n=2) === [EMAIL PROTECTED] == http://Jenda.Krynicky.cz == There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - Yahoo! Mail -- Une adresse @yahoo.fr gratuite et en français !
Re: remove duplicate values from array
Assuming that the array has been converted to a scalar, then the following Lookaround Assertion works: #!/usr/bin/perl $_ = 'a a a b b c c'; 1 while s/\b(\w+) \s (?= \1\b ) //gxi; print $_, \n; The scalar can be reconverted to an array. Not sure if this is any faster. Alfred, Project Breeze SNPS -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove duplicate values from array
On Monday, April 22, 2002, at 03:43 , Alfred Vahau wrote: Assuming that the array has been converted to a scalar, then the following Lookaround Assertion works: #!/usr/bin/perl $_ = 'a a a b b c c'; 1 while s/\b(\w+) \s (?= \1\b ) //gxi; print $_, \n; Survey Says: Benchmark: timing 2 iterations of Boink, Faq, Mine... Boink: 19 wallclock secs (18.58 usr + 0.00 sys = 18.58 CPU) @ 1076.43/s (n=2) Faq: 33 wallclock secs (31.72 usr + 0.00 sys = 31.72 CPU) @ 630.52/ s (n=2) Mine: 3 wallclock secs ( 3.74 usr + 0.00 sys = 3.74 CPU) @ 5347.59/s (n=2) upgrading jenda's original benchmark test so that we do not return the scalar in his, but do buy the conversion from an array to a scalar. assuming that it was in the scalar context the benchmark is: Benchmark: timing 2 iterations of Boink, Faq, Mine... Boink: 17 wallclock secs (15.70 usr + 0.00 sys = 15.70 CPU) @ 1273.89/s (n=2) ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove duplicate values from array
On Monday, April 22, 2002, at 11:53 , Nikola Janceski wrote: Gosh.. what ever happened to good ol' grep my %seen; @uniq = grep !$seen{$_}++, @array; http://www.wetware.com/drieux/CS/lang/Perl/Beginners/BenchMarks/ will show you that this works just slightly slower than just the hashing function But clearly way better than everything but jenda's cool piece. ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]