Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: foreach loop and creating files with "$_"

by xyzzy (Pilgrim)
on Jul 22, 2014 at 03:58 UTC ( #1094582=note: print w/replies, xml ) Need Help??

in reply to foreach loop and creating files with "$_"

First one quick note:

my $file = "file"; # why not... my $temp = "temp"; # makes sense if you later want to use # some other basename for your sequence of files foreach (1..5){ open $file, '>', "$temp$_"; # you just turned $file from a string in +to a filehandle... ... }

In other words your assignment in the first line does nothing since the value gets clobbered when you use open

On to your question. I quickly glanced at the node and the author is basically saying that sometimes it is unclear what $_ is supposed to represent. Whether or not this is the case depends on the context that you use it in. For instance:

foreach (@lines) { $_ = uc; # ALL CAPS WOOOOOO s/$/!!!/; # EVERY LINE IS AN EXCLAMATION!!! print; }
It's fairly obvious that each operation of the loop is performed on each line in @lines. However, this may not be the case if you have a longer, more complex loop, and will not work at all if you have nested loops:
my @timesTable; foreach my $row (0..12) { # foreach my $col (0..12) { @timesTable[$row][$col] = $row * $col; } }
There are also times where I will take advantage of $_ to perform a bunch of operations on a single variable in a sort of pseudo-loop:
for ($leetString) { # "loops" only once s/very/uber/g; tr/astloe/457103/; s!w!\\/\\/!g; }
You didn't hear it from me, but the best part about Perl is that once you understand how the lingo works, you can use it any way you damn please. Unless you work with people who won't understand what you're saying, and you want them to understand what you're saying. Then you have turn to these annoying things called "Best Practices".

$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
Happy, sober, smart: pick two.

Replies are listed 'Best First'.
Re^2: foreach loop and creating files with "$_"
by james28909 (Deacon) on Jul 22, 2014 at 04:07 UTC
    i didnt even catch the mistake at the beginning, i was just excited it worked i guess, glad you noticed tho loool xD

      Here's some more unrequested advice:

      for (1..$num) { open my $fh, '>', sprintf "temp%02d",$_; # temp01, temp02, etc. ... }
      Consider doing this if you plan to have more than 9 temp files and you want to keep them in numerical order. %02d will pad an integer with leading zeroes until it's two digits long. For more information, consult perldoc -f sprintf. You can even use a base-10 logarithm before the loop to figure out how many leading zeroes you would need, then insert that number into your format string.

      EDIT: forgot that $_ is not implied with sprintf

      $,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
      "My life is like my typing: fast and full of mistakes."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1094582]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2021-04-15 07:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found