Re: Array containment

2004-02-11 Thread Rob Dixon
James Edward Gray II wrote: > [other stuff and] > > Of the solutions not using exists, I come in third (range). That's not > > bad for an amateur or is it? ;) > > Not bad at all. Your solution was clever, I think, and may even have > practical uses, since it retains an array order in the hash va

Re: Array containment

2004-02-11 Thread James Edward Gray II
On Feb 11, 2004, at 9:23 AM, Jan Eden wrote: James Edward Gray II wrote: Now it might be hard to determine which of these two is faster. And the results, from my G5: Benchmark: timing 300 iterations of all_ones, foreach, hash_slice, map_hash, map_ones, range... all_ones: 1 wallclock secs

Re: Array containment

2004-02-11 Thread Jan Eden
James Edward Gray II wrote: >> Now it might be hard to determine which of these two is faster. >And the results, from my G5: > >Benchmark: timing 300 iterations of all_ones, foreach, hash_slice, >map_hash, map_ones, range... > all_ones: 1 wallclock secs ( 1.76 usr + 0.00 sys = 1.76 CPU

Re: Array containment

2004-02-11 Thread Rob Dixon
Jan Eden wrote: > > Rob, James, > > James Edward Gray II wrote: > > > > What's wrong with exists()? I like exists() and you're going to hurt > > it's feelings. :) > > > > It has six characters which can be left out to minimize typing. Proper Perl programmers like typing ;) And so do you given:

Re: Array containment

2004-02-11 Thread James Edward Gray II
On Feb 11, 2004, at 12:14 AM, Jan Eden wrote: James Edward Gray II wrote: On Feb 10, 2004, at 2:39 PM, Jan Eden wrote: Rob, I read the perlfaq paragraph you mentioned and found that it proposes a solution which does not make use of 'exists': What's wrong with exists()? I like exists() and you'r

Re: Array containment

2004-02-10 Thread Jan Eden
Rob, James, James Edward Gray II wrote: >On Feb 10, 2004, at 2:39 PM, Jan Eden wrote: >> Rob, I read the perlfaq paragraph you mentioned and found that it >> proposes a solution which does not make use of 'exists': > >What's wrong with exists()? I like exists() and you're going to hurt >it's

Re: Array containment

2004-02-10 Thread John W. Krahn
James Edward Gray II wrote: > > On Feb 10, 2004, at 2:39 PM, Jan Eden wrote: > > > > @blues = qw/azure cerulean teal turquoise lapis-lazuli/; > > %is_blue = (); > > for (@blues) { $is_blue{$_} = 1 } > > > > Now, I had the idea to combine your hash slice with this solution > > doing: > > > > @blues

Re: Array containment

2004-02-10 Thread Rob Dixon
Jan Eden wrote: > > Rob, I read the perlfaq paragraph you mentioned and found that it proposes a > solution which does not make use of 'exists': > > @blues = qw/azure cerulean teal turquoise lapis-lazuli/; > %is_blue = (); > for (@blues) { $is_blue{$_} = 1 } It's not for me to rewrite the docs (or

Re: Array containment

2004-02-10 Thread James Edward Gray II
On Feb 10, 2004, at 2:39 PM, Jan Eden wrote: James, Rob, Japhy, I am impressed, really. Thank you! Me too. We haven't scared you off yet. :) Impressive. Rob, I read the perlfaq paragraph you mentioned and found that it proposes a solution which does not make use of 'exists': What's wrong wit

Re: Array containment

2004-02-10 Thread Jan Eden
James, Rob, Japhy, I am impressed, really. Thank you! Rob, I read the perlfaq paragraph you mentioned and found that it proposes a solution which does not make use of 'exists': @blues = qw/azure cerulean teal turquoise lapis-lazuli/; %is_blue = (); for (@blues) { $is_blue{$_} = 1 } Now, I had

Re: Array containment

2004-02-10 Thread Rob Dixon
Jeff 'Japhy' Pinyan wrote: > > >I said I cannot fix the grep() version. I'm just not that cool. > > I guess I'm cooler than you. ;) > > sub find { > my $wanted = shift; > my $found = 0; > { grep $_ eq $wanted && ++$found && last, @_ } > return $found; > } Ouch! Nobody knows yo

Re: Array containment

2004-02-10 Thread Jeff 'japhy' Pinyan
On Feb 10, Jeff 'japhy' Pinyan said: >On Feb 10, James Edward Gray II said: > >>I said I cannot fix the grep() version. I'm just not that cool. > >I guess I'm cooler than you. ;) > > sub find { >my $wanted = shift; >my $found = 0; >{ grep $_ eq $wanted && ++$found && last, @_ } >

Re: Array containment

2004-02-10 Thread Jeff 'japhy' Pinyan
On Feb 10, James Edward Gray II said: >I said I cannot fix the grep() version. I'm just not that cool. I guess I'm cooler than you. ;) sub find { my $wanted = shift; my $found = 0; { grep $_ eq $wanted && ++$found && last, @_ } return $found; } -- Jeff "japhy" Pinyan

Re: Array containment

2004-02-10 Thread James Edward Gray II
On Feb 10, 2004, at 11:20 AM, Rob Dixon wrote: James wrote: On Feb 10, 2004, at 5:04 AM, Jan Eden wrote: Now I found a nicer solution in "Learning Perl Objects, References & Modules" and adapted it: sub contains { my $contained = shift; my $result = grep { $contained eq $_ } @_; } Again,

Re: Array containment

2004-02-10 Thread Rob Dixon
James wrote: > > On Feb 10, 2004, at 5:04 AM, Jan Eden wrote: > > > Now I found a nicer solution in "Learning Perl Objects, References & > > Modules" and adapted it: > > > > sub contains { > > my $contained = shift; > > my $result = grep { $contained eq $_ } @_; > > } > > Again, no need for

Re: Array containment

2004-02-10 Thread Jan Eden
James Edward Gray II wrote: >On Feb 10, 2004, at 8:53 AM, Jan Eden wrote: > >> Rob Dixon wrote: >> >>> @[EMAIL PROTECTED] = (); >>> >> I see. But shouldn't the last line be a complete hash slice: >> >> [EMAIL PROTECTED] = (); > >Rob's line is a hash slice. Note the @. Yours is a simple hash >

Re: Array containment

2004-02-10 Thread Jan Eden
James Edward Gray II wrote: >That works, though I would probably drop the extra variable. > >sub contains { > my $contained = shift; > foreach (@_) { return 1 if $_ eq $contained; } > return 0; >} > Perfect. Returns 1 as soon as it finds the element. >I'm not Randal, but will I do? > To

Re: Array containment

2004-02-10 Thread James Edward Gray II
On Feb 10, 2004, at 8:53 AM, Jan Eden wrote: Rob Dixon wrote: @[EMAIL PROTECTED] = (); I see. But shouldn't the last line be a complete hash slice: [EMAIL PROTECTED] = (); Rob's line is a hash slice. Note the @. Yours is a simple hash lookup, one scalar value affected, thus the $. James -

Re: Array containment

2004-02-10 Thread James Edward Gray II
On Feb 10, 2004, at 5:04 AM, Jan Eden wrote: Hi all, Howdy. a while ago, I wrote a little subroutine to test wether a given element is in a given array: sub contains { my $result = 0; my $contained = shift; foreach (@_) { if ($_ eq $contained){ $result = 1; } } $res

Re: Array containment

2004-02-10 Thread Jan Eden
Hi Rob, Rob Dixon wrote: >Hi Jan. > >If you want to test a static (unchanging) array for the containment of many >different >values you should build a hash out of the array elements: > > my @data = 'a' .. 'm'; > my %data_hash; > @[EMAIL PROTECTED] = (); > I see. But shouldn't the last line be

Re: Array containment

2004-02-10 Thread Rob Dixon
Jan Eden wrote: > > a while ago, I wrote a little subroutine to test wether a given element is in a > given array: > > sub contains { > > my $result = 0; > my $contained = shift; > foreach (@_) { > if ($_ eq $contained){ $result = 1; } > } > $result; > } > > Now I found

Array containment

2004-02-10 Thread Jan Eden
Hi all, a while ago, I wrote a little subroutine to test wether a given element is in a given array: sub contains { my $result = 0; my $contained = shift; foreach (@_) { if ($_ eq $contained){ $result = 1; } } $result; } Now I found a nicer solution in "Learning