Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Smart way to read a file vertically?

by johngg (Abbot)
on Jun 06, 2012 at 10:13 UTC ( #974682=note: print w/ replies, xml ) Need Help??

in reply to Smart way to read a file vertically?

Similar to other solutions but using push with a ternary and maps.

use strict; use warnings; use 5.010; open my $inFH, q{<}, \ <<'EOD' or die qq{open: < HEREDOC: $!\n}; NAME LAST_NAME PHONE EMAIL John Smith 1234 Peter Jones 6789 George Lukas 9086 EOD my( @N, @LN, @P, @E ); push @{ $_->[ 0 ] == 1 ? \ @N : $_->[ 0 ] == 2 ? \ @LN : $_->[ 0 ] == 3 ? \ @P : \ @E }, $_->[ 1 ] for map { chomp; my $col = 0; map { [ ++ $col, $_ ] } split m{\t}; } <$inFH>; close $inFH or die qq{close: < HEREDOC: $!\n}; say for @N, q{}, @LN, q{}, @P, q{}, @E;

The output.

NAME John Peter George LAST_NAME Smith Jones Lukas PHONE 1234 6789 9086 EMAIL

I hope this is of interest.

Update: As pointed out by Not_a_Number, the code would have failed for some names. I had forgotten to put tabs back into the data after the copy'n'paste put in the displayed spaces :-(

Code now corrected.



Comment on Re: Smart way to read a file vertically?
Select or Download Code
Replies are listed 'Best First'.
Re^2: Smart way to read a file vertically?
by Not_a_Number (Parson) on Jun 06, 2012 at 21:50 UTC

    Fails for 'Mary Lou Retton' and 'Ludwig van Beethoven'

      If we consider a slightly different situation: Say the first names are in one file, last names in another, and so on. How might one be able to read in from those files and print out to a master list with each column of data side by side? I could read it in and print each file line by line, but I want to print them column by column and don't really know how to do that.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2015-12-01 18:41 GMT
Find Nodes?
    Voting Booth?

    My keyboard shows this many letters:

    Results (25 votes), past polls