Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Bash for loop variable pass equivalent in perl

by ZWcarp (Beadle)
on Aug 10, 2012 at 16:32 UTC ( #986769=perlquestion: print w/replies, xml ) Need Help??
ZWcarp has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to replicate something like bash's

for i in `cat Overlap.txt`; do echo "$i";done in perl ...

I tried ... perl -lane 'print $_ for `cat Overlap.txt`' but this did not work. Any suggestions on how to do that in perl command line


Replies are listed 'Best First'.
Re: Bash for loop variable pass equivalent in perl
by BrowserUk (Pope) on Aug 10, 2012 at 16:37 UTC
    perl -ple1 Overlap.txt

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Cool. I've always done the longer:

      perl -lne 'print' Overlap.txt

      Update: And even shorter (and more arbitrary):

      perl -pe3 Overlap.txt
Re: Bash for loop variable pass equivalent in perl
by aitap (Curate) on Aug 10, 2012 at 17:05 UTC
    In addition to the previous comments, using for var in `cat file` is usually wrong. The file will not only be split by newlines, but by other symbols (defined in shell variable IFS; spaces, newlines and tabs by default) too. If you need similar behaviour, you need perl -lpe 'join "\n",split/[\t ]/' file.
    EDIT: fixed code
    Sorry if my advice was wrong.

      And to get perl's default line-by-line behavior in the shell, set IFS to a newline (The ctrl-V ctrl-J sequence inserts a raw newline into the command without ending it):

      $ IFS="^V^J "; for i in `cat file`; do echo aaa $i; done

      Aaron B.
      Available for small or large Perl jobs; see my home node.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://986769]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-05-27 22:33 GMT
Find Nodes?
    Voting Booth?