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

Re: Common Perl Idioms

by Anonymous Monk
on Jul 23, 2004 at 19:20 UTC ( [id://376964]=note: print w/replies, xml ) Need Help??


in reply to Common Perl Idioms

Some of the ones I can remember off the top of my head...

# slurp a bunch of files @files = { local(@ARGV, $/) = @filenames; <> }; # get the indexes of items in an array @foo{ @foo } = 0 .. @foo; $foo{'abc'} # the index of 'abc' in @foo # Get the sign of a number $a <=> 0 # test primality sub isPrime { (1 x $_[0]) !~ /^(11+)\1+$/ } # (ok I never use that in production code # but it is funny)

Ted

Replies are listed 'Best First'.
Re^2: Common Perl Idioms
by revdiablo (Prior) on Jul 23, 2004 at 21:19 UTC

    Your slurp:

    @files = { local(@ARGV, $/) = @filenames; <> }; # missing 'do'

    Should actually be:

    @files = do { local(@ARGV, $/) = @filenames; <> };

    And your index hash:

    @foo{ @foo } = 0 .. @foo; # off-by-one error

    Should be:

    @foo{ @foo } = 0 .. $#foo;

      You are right about the slurp, a typo on my part.

      But the indexing will work. The extra number (int @foo) will just be ignored because there won't be a corresponding lvalue.

      Ted :->
Re^2: Common Perl Idioms
by I0 (Priest) on Jul 24, 2004 at 07:10 UTC
    1 is not prime. Neither is 0.
Re^2: Common Perl Idioms
by davorg (Chancellor) on Jul 24, 2004 at 06:36 UTC
    # Get the sign of a number
    $a <=> 0

    Or just use "abs".

    Ignore me. I'm talking nonsense.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Or just use "abs".

      Eh?

Re^2: Common Perl Idioms
by Anonymous Monk on Jul 27, 2004 at 14:48 UTC
    The @foo{ @foo } = 0 .. @foo; seems very clever to me (not to mention useful), but being a bit of a newbie, I don't really understand how it works. Anyone care to enlighten me?
      It's creating a hash named %foo using a hash slice built out of the elements in @foo. Then it's assigning to that slice the values 0 to (the number of elements). So if @foo = ('a', 'b', 'c'), you get
      @foo{'a', 'b', 'c'} = (0, 1, 2);
      which is itself just shorthand for
      $foo{'a'} = 0; $foo{'b'} = 1; $foo{'c'} = 2;
      (technically, @foo in scalar context returns a value one too large, so we're really mapping to (0,1,2,3), but the odd element gets ignored. It might be more proper to use $#foo there (the index of the last element, instead of the number of elements))
        Aaaaaaaah... That was clever. Thanks!
      You want to look up hashslices. That's going to be the key.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-18 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found