Oh. Where to begin. Okay, I'm going to (try) to respond to both your posts here.

The command line:perl -n -e" print " yourFile causes perl to read (-n) yourFile one line at a time, and execute the inline script (the -e" ... " bit) for each line, passing the line in the variable: $_.

$_ is the 'default variable' upon which many of Perl's built-in functions will operate if they are not given any arguments. Eg print

So, in the above case, all that does is print each line to the terminal. The effect is the same aas type yourFile or cat yourFile depending upon your operating system. Not so useful, but a useful simple example.

If you add the switch -MO=Deparse to the above command, Perl displays the full script equivalent that it runs on your behalf.

In the case of the above, when you type the command: perl -n -e" print " yourFile, Perl actually executes:

c:\test>perl -MO=Deparse -n -e" print " yourFile LINE: while (defined($_ = <ARGV>)) { print $_; } -e syntax OK

What that tells you is that Perl opens yourFile as the filehandle ARGV, and then reads a line at a time from that file handle and places it into $_. It then invokes the -e code, which is just print, which causes it to be printed to the screen. Where it could be redirected to another file.

In the case of the one-liner I posted, the output (with my annotations) is :

## Set the input separator to be '>'; and the output separator to be " +\n"' BEGIN { $/ = ">"; $\ = "\n"; } ## read each input record (terminated by the '>' character) into $_ LINE: while (defined($_ = <ARGV>)) { ## Remove the record terminator ('>') chomp $_; ## split the record on whitespace into @F our(@F) = split(' ', $_, 0); ## If the record was empty (the first one always will be with your dat +a), ## then skip to the next record. next unless @F; ## remove the 'length=' prefix from the second field s/length=// foreach ($F[1]); ## replace the 6th field in the array, with the concatenation of itsel +f ## and all the fields that follow it @F[5] = join('', @F[5..99]); ## Then discard all the fields that follow it. ## Ie. truncate the array after the 6th field $#F = 5; ## And print out the fields joined with ','s and prefixed with '>' ## (and ending with a newline. See output separator above.) print '>', join(',', @F); }

Either you'll read perlrun, understand the options used on the one-liner and make sense of all that and it will help you. Or you won't and it was a waste of both our time.


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?


In reply to Re^5: Perl script help to convert .txt file to .csv by BrowserUk
in thread Perl script help to convert .txt file to .csv by Seabass

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.