rusty@groan.Berkeley.EDU (Rusty Wright) (05/15/91)
I've used a language where with arrays there's an "in" operator that checks to see if an element is in an arry. For example, if you do @array = ( 1, 2, 3 ); if ( 1 in @array ) { print "true\n"; } it would print true. Does perl have anything like this?
arielf@tasu8c.UUCP (Ariel Faigon) (05/15/91)
+--- In the ref. article rusty@groan.Berkeley.EDU (Rusty Wright) wrote: | I've used a language where with arrays there's an "in" operator that | checks to see if an element is in an arry. For example, if you do | | @array = ( 1, 2, 3 ); | | if ( 1 in @array ) { print "true\n"; } | | it would print true. Does perl have anything like this? +--- Sure it has. Only the syntax is a bit different... What you want is an associative array: %array = (1,1, 2,1, 3,1); # pairs of key,value... print "true\n" if ($array{1}); If you don't want to initialize the whole array at once, you can also use: $array{1} = 1; $array{2} = 1; $array{3} = 1; print "true\n" if ($array{1}); -- --- Ariel Faigon National Semiconductor Corp. (NSTA Design Center) 6 Maskit St. P.O.B. 3007, Herzlia 46104, Israel Tel. +972 52-522272
merlyn@iwarp.intel.com (Randal L. Schwartz) (05/15/91)
In article <RUSTY.91May14133645@groan.Berkeley.EDU>, rusty@groan (Rusty Wright) writes: | I've used a language where with arrays there's an "in" operator that | checks to see if an element is in an arry. For example, if you do | | @array = ( 1, 2, 3 ); | | if ( 1 in @array ) { print "true\n"; } | | it would print true. Does perl have anything like this? @array = ( 1, 2, 3 ); print "found one\n" if grep($_ == 1, @array); print "found four\n" if grep($_ == 4, @array); Remember that grep() returns the number of matches in a scalar context. (Note for the JAPH: For those of you that have been waiting for a better JAPH comparable to my earlier works, check this one out... look carefully... I truly am selecting one of the funny lines at random.) srand;eval join("",reverse sort split(/:/,(split(/\n/,<<'-- '))[rand(32)])) nother Perl hac:,":print "Just a:ker hacker,":nother Perl:print "Just a nother Perl hack:er,":print "Just a print "Just anot:her Perl ha:":cker, other Perl hac:ker,":print "Just an another Perl: hacker,":print "Just Perl hacker,": another:print "Just print "Just an:other :Perl hacker," l hac:ker:other Per:print "Just an:," print "Just an:l hac:ker,":other Per other Perl ha:cker,":print "Just an print "Just a:nother Perl :hacker," acker,":other Perl h:print "Just an nother P:print "Just a:erl hacker," print "Just a:l hacker,":nother Per acker:,":nt "Just anot:pri:her Perl h pr:Perl hacker,":her :int "Just anot print "Just a:cker,":nother Perl ha l hacker,:other Per:print "Just an:" her P:not:print "Just a:erl hacker," ":print "Just anot:her Perl hacker, er,":her Perl hack:print "Just anot cker,:other Perl ha:print "Just an:" "Just another:print : Perl hacker," her Perl ha:print "Just an:cker,":ot not:her Perl hacker,:print "Just a:" print "Just a:acker,":nother Perl h print "Just anot:,":her Perl hacker nother :print "Just a:Perl hacker," oth:cker,":er Perl ha:print "Just an Perl hacker,":nother:print "Just a her Perl hacker:nt "Just anot:pri:," -- -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/
jpn@genrad.com (John P. Nelson) (05/16/91)
In article <RUSTY.91May14133645@groan.Berkeley.EDU> rusty@groan.Berkeley.EDU (Rusty Wright) writes: >I've used a language where with arrays there's an "in" operator that >checks to see if an element is in an arry. For example, if you do > > @array = ( 1, 2, 3 ); > > if ( 1 in @array ) { print "true\n"; } > >it would print true. Does perl have anything like this? Well, sort of. "grep" can be used to implement this fairly easily using an expression that contains ==: if (grep(1 == $_, @array)) { print "true\n"; } This works essentially the same as your "in" operator. Unfortunately, "grep" doesn't stop the first time it finds a match, it iterates over the whole array no matter what. john nelson uucp: {decvax,mit-eddie}!genrad!jpn domain: jpn@genrad.com
merlyn@iwarp.intel.com (Randal L. Schwartz) (05/16/91)
In article <WEG.91May15141708@convx1.convx1.ccit.arizona.edu>, weg@convx1 (Eythan Weg) writes: | Can this work too? | | @array = ( 1, 2, 3 ); | print "found one\n" if grep(/1/, @array); | print "found four\n" if grep(/4/, @array); Depends on what you mean by "work"... @array = ( 1, 2, 3, 14 ); print "found four\n" if grep(/4/, @array); prints "found four", because /4/ is true on "14". print "Just another Perl hacker," # rushing to a meeting :-) -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/
weg@convx1.ccit.arizona.edu (Eythan Weg) (05/16/91)
In article <1991May15.153547.9282@iwarp.intel.com> merlyn@iwarp.intel.com (Randal L. Schwartz) writes: In article <RUSTY.91May14133645@groan.Berkeley.EDU>, rusty@groan (Rusty Wright) writes: | I've used a language where with arrays there's an "in" operator that | checks to see if an element is in an arry. For example, if you do | | @array = ( 1, 2, 3 ); | | if ( 1 in @array ) { print "true\n"; } | | it would print true. Does perl have anything like this? @array = ( 1, 2, 3 ); print "found one\n" if grep($_ == 1, @array); print "found four\n" if grep($_ == 4, @array); Remember that grep() returns the number of matches in a scalar context. Can this work too? @array = ( 1, 2, 3 ); print "found one\n" if grep(/1/, @array); print "found four\n" if grep(/4/, @array); Eythan
tchrist@convex.COM (Tom Christiansen) (05/16/91)
From the keyboard of rusty@groan.Berkeley.EDU (Rusty Wright): :I've used a language where with arrays there's an "in" operator that :checks to see if an element is in an arry. For example, if you do : : @array = ( 1, 2, 3 ); : : if ( 1 in @array ) { print "true\n"; } : :it would print true. Does perl have anything like this? Not as such. Here's question #26 from the FAQ: 26) How can I test whether an array contains a certain element? There are several ways to approach this. If you are going to make this query many times and the values are arbitrary strings, the fastest way is probably to invert the original array and keep an associative array around whose keys are the first array's values. @blues = ('turquoise', 'teal', 'lapis lazuli'); undef %is_blue; grep ($is_blue{$_}++, @blues); Now you can check whether $is_blue{$some_color}. It might have been a good idea to keep the blues all in an assoc array in the first place. If the values are all small integers, you could use a simple indexed array. This kind of an array will take up less space: @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31); undef @is_tiny_prime; grep($is_tiny_prime[$_]++, @primes); Now you check whether $is_tiny_prime[$some_number]. If the values in question are integers, but instead of strings, you can save quite a lot of space by using bit strings instead: @articles = ( 1..10, 150..2000, 2017 ); undef $read; grep (vec($read,$_,1) = 1, @articles); Now check whether vec($read,$n,1) is true for some $n. -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."
tchrist@convex.COM (Tom Christiansen) (05/16/91)
From the keyboard of weg@convx1.ccit.arizona.edu (Eythan Weg): :Can this work too? : : @array = ( 1, 2, 3 ); : print "found one\n" if grep(/1/, @array); : print "found four\n" if grep(/4/, @array); : Consider: @array = ( 2..20 ); print "found one\n" if grep(/1/, @array); --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."