in reply to A C-like brain in a Perl-like world
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: A C-like brain in a Perl-like world
by CheeseLord (Deacon) on Sep 27, 2001 at 05:08 UTC | |
by ton (Friar) on Sep 27, 2001 at 05:31 UTC | |
by $code or die (Deacon) on Sep 27, 2001 at 05:52 UTC | |
by tilly (Archbishop) on Sep 27, 2001 at 07:24 UTC | |
by tachyon (Chancellor) on Sep 27, 2001 at 15:16 UTC |