You do it using a hash like this - it is rather brief :-) but that's perl for you:
sub merge { my %hash; $hash{$_}++ for @_; return keys %hash; } sub intersection { my %hash; $hash{$_}++ for @_; return grep { $hash{$_} > 1 } keys %hash } sub lookup { $value = shift; for (@_) { return 1 if $value eq $_; } return 0; } @ary1 = qw( a b c d e f g ); @ary2 = qw( e f g h i j k ); print merge ( @ary1, @ary2), "\n"; print intersection ( @ary1, @ary2 ), "\n"; for ( qw( j a p h ) ) { print lookup( $_, @ary2) ? "$_ Found\n" : "$_ Not found\n"; }
There is a thing called the Perl FAQ which is nine documents covering all the common stuff you will want to do like merge arrays, find intersections.....
By the way it will be a good idea to lose the TRUE FALSE habit. In perl everything is true except for:
This is really handy as it lets you do stuff like print @array if @array which will only print @array if it contains elements and is thus true or &some_func if $flag which will only call the sub if $flag is true. This also means that unless you have defined a sub sub FALSE { 0 } that when you think you return FALSE you don't.
sub oops { return FALSE; } print "Oops 'FALSE' is true in Perl" if &oops;
CheeseLord points out a error in my understanding. Thanks!
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In reply to Re: A C-like brain in a Perl-like world
by tachyon
in thread A C-like brain in a Perl-like world
by cyberscribe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |