Re: if element exists in an array

2016-08-19 Thread Chas. Owens
On Fri, Aug 19, 2016 at 2:22 PM Chas. Owens wrote: > Truth. If you are checking in lots of things exist a hashset might be a > better way to go: > > my %hashset = map { ($_ => undef) } (3,1,4,2,9,0); > > my $found = exists $hashset{4} || 0; > my $not_found = exists $hashset{10} || 0; > > By sett

Re: if element exists in an array

2016-08-19 Thread Chas. Owens
Truth. If you are checking in lots of things exist a hashset might be a better way to go: my %hashset = map { ($_ => undef) } (3,1,4,2,9,0); my $found = exists $hashset{4} || 0; my $not_found = exists $hashset{10} || 0; By setting the value of the hash to be undef, you take up less space than s

Re: if element exists in an array

2016-08-19 Thread wagsworld48 via beginners
But does it need to be an array. Rethink into hash and life could be a little bit easier... Wags ;) WagsWorld Hebrews 4:15 Ph: 408-914-1341 On Aug 18, 2016, 19:41 -0700, kp...@freenet.de, wrote: > Thanks for all the replies. > Yes I found List::Util is a useful toolset. > > > On 2016/8/19 10:00,

Re: if element exists in an array

2016-08-18 Thread kpeng
Thanks for all the replies. Yes I found List::Util is a useful toolset. On 2016/8/19 10:00, Chas. Owens wrote: The any function from List::Util will also do what you want. perldoc List::Util http://perldoc.perl.org/List/Util.html#any my $found = any { $_ == 4 } (3, 1, 4, 2, 9, 0); # true my

Re: if element exists in an array

2016-08-18 Thread Chas. Owens
On Thu, Aug 18, 2016 at 9:39 PM wrote: > Hello, > > What's the better way to decide if an element exists in an array? > Something like what ruby does, > > irb(main):001:0> x=[3,1,4,2,9,0] > => [3, 1, 4, 2, 9, 0] > irb(main):002:0> x.include? 4 > => true > irb(main):003:0> x.include? 10 > => fals

Re: if element exists in an array

2016-08-18 Thread Chris Fedde
Here is one approach using a perl repl. re.pl $ my @x = qw(3 1 4 2 9 0) $VAR1 = 3; $VAR2 = 1; $VAR3 = 4; $VAR4 = 2; $VAR5 = 9; $VAR6 = 0; $ grep {$_ == 4} @x and 'true' grep {$_ == 10} @x and 'true' or 'false' On Thu, Aug 18, 2016 at 7:35 PM, wrote: > Hello, > > What's the better way to decide

Re: if element exists in an array

2016-08-18 Thread Kent Fredric
On 19 August 2016 at 13:45, Kent Fredric wrote: >if ( first { $item == 4 } @items ) { >say "Yes"; >} > Ugh, my bad. if ( first { $_ == 4 } @items ) { say "Yes"; } -- Kent KENTNL - https://metacpan.org/author/KENTNL -- To unsubscribe, e-mail: beginners-unsub

Re: if element exists in an array

2016-08-18 Thread Kent Fredric
On 19 August 2016 at 13:35, wrote: > > What's the better way to decide if an element exists in an array? > Something like what ruby does, > > irb(main):001:0> x=[3,1,4,2,9,0] > => [3, 1, 4, 2, 9, 0] > irb(main):002:0> x.include? 4 > => true > irb(main):003:0> x.include? 10 > => false > irb(main)

if element exists in an array

2016-08-18 Thread kpeng
Hello, What's the better way to decide if an element exists in an array? Something like what ruby does, irb(main):001:0> x=[3,1,4,2,9,0] => [3, 1, 4, 2, 9, 0] irb(main):002:0> x.include? 4 => true irb(main):003:0> x.include? 10 => false irb(main):004:0> quit I tried searching but found nothin