Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Teaching Perl Idioms

by VSarkiss (Monsignor)
on Sep 26, 2001 at 21:51 UTC ( [id://114880]=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    # suppose @myvec is an array of integers.
    my $sum = 0;
    for (my $i = 0; $i < scalar(@myvec); $i++)
    {
        $sum += 2 * $myvec[$i];
    }
    
  2. or download this
    my $sum = 0;
    foreach my $i (@myvec)
    {
        $sum += 2 * $i;
    }
    
  3. or download this
    my $sum = 0;
    $sum += 2 * $_ foreach @myvec;
    
  4. or download this
    $sum = eval join('+', map { 2 * $_ } @myvec);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://114880]
help
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: (None)
    As of 2024-04-19 00:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found