Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How can we interpolate an expression??

by Rohit Jain (Sexton)
on Sep 20, 2012 at 17:53 UTC ( #994707=perlquestion: print w/ replies, xml ) Need Help??
Rohit Jain has asked for the wisdom of the Perl Monks concerning the following question:

I want to use an expression in the print statement itself, without having to store the result in a temp variable.. Something like the below code: -
use v5.14; my $number = 5; my $multiplier = 3; # I tried the below code.. But niether is working.. say "Product is $number * $multiplier"; say "Product is {$number * $multiplier}";
How do we exactly do this in Perl??

Comment on How can we interpolate an expression??
Download Code
Re: How can we interpolate an expression??
by Anonymous Monk on Sep 20, 2012 at 17:55 UTC

    Can you do it without interpolation and without temporary variable?

      Yes.. I can use single quotes and '.' to concatenate the results just as we do in Java..
      say 'Result is :' . $num * $multipler;
        my $arrayref = [ 4200 / 100 ]; print "The answer is $arrayref \n";
        The answer is ARRAY(0x3f8a94)

        my $arrayref = [ 4200 / 100 ]; print "The answer is @$arrayref \n";
        The answer is 42

        my $arrayref = ; print "The answer is @[ 4200 / 100 ] \n";
        syntax error at - line 1, near "= ;" Execution of - aborted due to compilation errors.

        print "The answer is @{[ 4200 / 100 ]}\n";
        The answer is 42
Re: How can we interpolate an expression??
by toolic (Chancellor) on Sep 20, 2012 at 18:04 UTC
    use warnings; use strict; use 5.14.0; my $number = 5; my $multiplier = 3; say "Product is $number * $multiplier"; say "Product is ", $number * $multiplier; __END__ Product is 5 * 3 Product is 15
      Can't we make your first "say ..." statement to print like the second one?? When we do this: -
      say "number is $number";
      It simply replaces the $number with its value.. Shouldn't it do the same for an expression.. or we have to do it in some different way?
        If the interpreter implemented the interpolation you are specifying, then how could I as a programmer make it output the string Product is 5 * 3? There'd be some pretty non-intuitive side effects.

        Kenosis below has given you a way to be clever, but this is all getting a little clever for my taste. For complex output, I usually prefer going all the way to printf since, after all, you desire a formatted print. So maybe printf "number is %d\n", $number * $multiplier;. toolic's solution still strikes me as pretty clean as well.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: How can we interpolate an expression??
by Kenosis (Priest) on Sep 20, 2012 at 18:11 UTC

    Try the following:

    use Modern::Perl; my $number = 5; my $multiplier = 3; say "Product is @{[$number * $multiplier]}";

    Output:

    Product is 15

      What I understand from this code is: -

      First we are converting the expression into an array or list.. Now, had we printed out that list or array, it would have printed out its address.. So, we have added an '@' before it to get an array.. Now, because we are not using a reference variable to this list, we have used a curly braces before using '@'

      Have I interpreted it correct.. Is this what happens in Perl?? Or can I get a better explanation of what is happening there?

        Great understanding!

        say "Product is @{[$number * $multiplier]}"; ^ ^ ^ ^ | | | | | | | + - EXPRESSION | | + - reference | + - dereference + - interpolate
Re: How can we interpolate an expression??
by Rohit Jain (Sexton) on Sep 20, 2012 at 18:25 UTC
    Yeah.. got it worked with that set of "Curly" braces and "Square" brackets around the expression.. Thanks everyone :)
Re: How can we interpolate an expression??
by ww (Bishop) on Sep 20, 2012 at 18:51 UTC

    ... as a one-liner; nothing else here is new, other than escaping the variables to avoid interpolation.

    C:\>perl -E "my $num1=3; my $num2=4; say \"The result of \$num1 * \$nu +m2 is: \" . $num1*$num2;" The result of $num1 * $num2 is: 12
Re: How can we interpolate an expression??
by Marshall (Prior) on Sep 21, 2012 at 07:43 UTC
    my $number = 5; my $multiplier = 3; # Use print instead of "say" print "Product is ", $number * $multiplier, "\n"; __END__ Product is 15
    "say" is a short hand way to add the "\n" at the end of the line.
    when you don't mean or need that, just old fashioned print will work great.

      Well, the only one different that I know between print and say is that: -

      -->"say" adds a Newline to the output, whereas we have to give it explicitly in "print".

      Is there any other different??

        my $number = 5; my $multiplier = 3; # Use print instead of "say" print "Product is ", $number * $multiplier, "\n"; #this does not work... #"say" has limitations... say "Product is ", $number * $multiplier; __END__ Product is 15
        "Say" will add a \n to a simple string. More complex things require "print" or "printf".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994707]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-09-18 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls