Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: print and say

by Anonymous Monk
on Sep 12, 2017 at 09:13 UTC ( #1199177=note: print w/replies, xml ) Need Help??


in reply to print and say

Ok, for #2 I found the answer, say just prints a newline by default.
So I am basically down to figuring out why I cannot store the result using:
$answer = myfunct($str1, $str2);

It does not print anything, while with:
say for myfunct($str1, $str2);

or
print for myfunct($str1, $str2);

it gives me the result.

Replies are listed 'Best First'.
Re^2: print and say
by roboticus (Chancellor) on Sep 12, 2017 at 16:38 UTC

    Anonymous Monk:

    Your first code snippet stores the result in $answer, but you haven't told it to print yet. Here's a quickie bit of code to show you a few variations on the theme:

    use strict; use warnings; use v5.20; my ($a_val, @some_vals); # Store a scalar $a_val = scalar_func(10); # Print it later say "A random number from 1 .. 10:"; say $a_val; # Store a list @some_vals = list_func(5, 10); say "Print a list of values, each on one line"; say for @some_vals; say "Print all the values on a single line"; say @some_vals; say "Print all of them on a single line, with commas"; say join(", ", @some_vals); # return a random integer from 1 to the specified number sub scalar_func { my $range = shift; return 1 + int($range * rand); } # return $count random numbers from 1 .. $range sub list_func { my ($count, $range) = @_; return map { scalar_func($range) } 1 .. $count; }

    Will print something like:

    A random number from 1 .. 10: 1 Print a list of values, each on one line 3 1 10 10 3 Print all the values on a single line 3110103 Print all of them on a single line, with commas 3, 1, 10, 10, 3

    Also, LanX is right: you don't want to use the 'for' clause when your function returns a single value. It doesn't do anything wrong in this case, but it's misleading: When reading the code, it implies that your function is returning a list of results.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re^2: print and say
by LanX (Bishop) on Sep 12, 2017 at 09:16 UTC
    for does a loop over one element, that's nonsense.

    $answer = myfunct($str1, $str2); say $answer;

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Disregarding efficiency, a for loop over one element can be "useful" as a "topicalizer", i.e. locally setting $_ and shortening code. It's suggested by the Perl documentation in Switch Statements for use with the experimental when.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1199177]
help
Chatterbox?
LanX .oO( cemetery breweries ? )

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2017-12-15 11:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (431 votes). Check out past polls.

    Notices?