Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Welcome to the Monastery
 
PerlMonks  

Re: extracting columns

by jose_m (Acolyte)
on Mar 22, 2012 at 18:54 UTC ( #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";


Comment on Re: extracting columns
Download Code
Re^2: extracting columns
by GrandFather (Cardinal) 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
Node Status?
node history
Node Type: note [id://961078]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2014-04-23 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (551 votes), past polls