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

Re: Perl Syntax

by davido (Cardinal)
on Oct 30, 2013 at 21:19 UTC ( [id://1060450]=note: print w/replies, xml ) Need Help??


in reply to Perl Syntax

0+ is a numeric operation; add 0 to $new. And $new contains an array reference, which stringifies as something like ARRAY(0x1427fc8), and numerifies as something like 28299208. So 0+ forces a numeric representation of the array reference held in $new.

Far more than you ever wanted to know (but you did ask, so...) about stringification and numerification is detailed in perldata. Perl's "duck typing" means that if you treat something like a string, Perl will do what it can to make a string of it. If you treat something as a number, Perl will do what it can to make a number of it.


Dave

Replies are listed 'Best First'.
Re^2: Perl Syntax
by kbfiles (Initiate) on Oct 30, 2013 at 22:01 UTC

    Thank you Dave. I thought it was doing something like as you explained. I just needed some confirmation from more experienced people like you. In regards to my other question, what is the syntax doing when you use:

     $hash{$_} = $new for $a, $b;

    Is this assigning the value of $a and $b as keys for $new as the value in the hash table?

      Inside a foreach loop (for is synonymous with foreach) the $_ variable is aliased to each element of the list over which you are iterating. Sometimes it's called the "topic" variable, because it is the topic of each loop iteration unless you explicitly specify another variable. The code you demonstrated is equivalent to this:

      foreach ( $a, $b ) { $hash{$_} = $new; }

      ...which is about the same as...

      foreach my $item_alias ( $a, $b ) { $hash{$item_alias} = $new; }

      ...which can be unrolled as...

      $hash{$a} = $new; $hash{$b} = $new;

      A foreach loop that contains a simple statement in its block can be inverted to $hash{$_} = $new foreach $a, $b;, and further shortened with for. This should be discussed in greater (and possibly more accurate) detail in perlsyn.


      Dave

      It is equivalent to:
      $hash{$a} = $new; $hash{$b} = $new;

                   When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

Log In?
Username:
Password:

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

    No recent polls found