Syntactic Confectionery Delight PerlMonks

### comment on

 Need Help??

Seems to be a problem with references ...

The map operator receives all the elements of the array (x, y, z) and passes all of them correctly to the function f2:

```#! /usr/perl -l

sub f1 {
my (\$f) = @_;
my @z = ("\$f");
for my \$d (@z) {return \$d}
}

sub f2 {
my (\$f) = @_;
print "\n*\$f*\n"
for my \$d ("\$f") {return \$d}
}

sub f3 {
for my \$d (@_) {return \$d}
}

print map {f1(\$_)} qw(x y z);
print map {f2(\$_)} qw(x y z);
print map {f3(\$_)} qw(x y z);
Extrange, is like the function f2, only has been collecting the ref of the \$d variable, and when print is going to print the array, it prints the value of the same reference; that is the last 'z' (what's about the scopes!!! arrg)

I have solved the problem using a intermediate variable:

```#! /usr/perl -l

sub f1 {
my (\$f) = @_;
my @z = ("\$f");
for my \$d (@z) {return \$d}
}

sub f2 {
my (\$f) = @_;
for my \$d ("\$f") {my \$a=\$d; return \$a}
}

sub f3 {
for my \$d (@_) {return \$d}
}

print map {f1(\$_)} qw(x y z);
print map {f2(\$_)} qw(x y z);
print map {f3(\$_)} qw(x y z);

cheers

perl -Te 'print map { chr((ord)-((10,20,2,7)[\$i++])) } split //,"turo"'

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2022-05-24 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you prefer to work remotely?

Results (84 votes). Check out past polls.

Notices?