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

Re: extracting columns

by jose_m (Acolyte)
on Mar 22, 2012 at 18:54 UTC ( [id://961078]=note: print w/replies, xml ) Need Help??


in reply to extracting columns

i would read the file into an array its cleaner and easier to read

#!/usr/bin/perl my $file="file.txt"; open (FH, "< $file") or die "$!"; while (<FH>) { push (@lines, $_); } close FH or die "$!"; print "@lines[0]\t@lines[1]\n\n";

Replies are listed 'Best First'.
Re^2: extracting columns
by GrandFather (Saint) on Mar 22, 2012 at 20:46 UTC
    its cleaner and easier to read

    Why? It's not cleaner because it introduces an extra layer of array handling. Your rendition is not easier to read (for me at least) because the indentation is bad.

    You have also missed strictures which the OP++ had and you don't use lexical file handles which the OP++ also had. You should use the three parameter version of open and it is good to have the die mention the name of the file being opened or created as well as showing the system error message.

    print "@lines[0]\t@lines[1]\n\n"; should be written print "$lines[0]\t$lines[1]\n\n";.

    Your code does not actually do what the OP wants to achieve. The OP wants to extract selected fields from a data file and generate a new file. Your code reads pairs of lines from a file then prints them out as pairs of lines with two blank lines between them and a tab prepended to the second line of each pair.

    Don't just invent stuff and expect it to work!

    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found