Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

  • 0 (including the string equvalents "0" which evaluates to zero)
  • '' - the null string
  • undef - an undefined value
  • () - an empty list/array

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;

Update

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 18:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found