Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: A C-like brain in a Perl-like world

by tachyon (Chancellor)
on Sep 26, 2001 at 23:23 UTC ( [id://114913]=note: print w/replies, xml ) Need Help??


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:

  • 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

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

    In perl everything is true except for:

    0 (including the string equvalents "0" "0.0" etc which all evaluate to zero)

    Sorry, tachyon, but that's not quite right. "0.0" is not false in perl:

    % perl -le 'print "" ? "True" : "False"' False % perl -le 'print "0" ? "True" : "False"' False % perl -le 'print "0.0" ? "True" : "False"' True

    Now with new grammatical goodness! (Thanks, blakem. ;-)

    His Royal Cheeziness

      Apparently Perl is not as seemless in its conversions between strings and floating point numbers as it is in its conversions between strings and integers. Because the following works:

      % perl -le '$foo=0.0; print $foo ? "True" : "False";' False
      but the following does not:
      % perl -le '$foo="0.0"; print $foo ? "True" : "False";' True

      -----
      Be bloody, bold, and resolute; laugh to scorn
      The power of man...
        Perl seems to be assuming that "0.0" is a string, which is fair enough, if that is by design, which I assume it is. "return 0.0" would from a subroutine would evaluate as false.

        Perl will convert "0.0" to a number as soon as you do anything mathematical on it. Such as:
        perl -e '$foo = "0.0" + 0; print $foo ? "True" : "False"'
        Which behaves as you'd expect. This reminds me of (even though it's completely different):
        perl -e 'print "I cannot add" unless 19.08 + 2.01 == 21.09'
        If I remember correctly this comes from an early TPJ article.

        Update: removed ' from print statement - thanks tilly - That's me in hybrid Win32 cmd.exe, Linux shell quoting mode :)

        Simon Flack ($code or die)
        $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
        =~y'_"' ';eval"die";print $_,lc substr$@,0,3;

      Hey good point! "0.0" is only zero if you force an eval on it one way or another. These are interesting:

      print "0.0 is == 0\n" if "0.0" == 0; $string_zero = "0.0"; print "True" if $string_zero; print "True" if eval $string_zero;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://114913]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found