Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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 lala@lala.com Peter Jones 6789 ttt@yahoo.com George Lukas 9086 lll@hotmail.com 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 lala@lala.com ttt@yahoo.com lll@hotmail.com

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.

Cheers,

JohnGG


Comment on Re: Smart way to read a file vertically?
Select or Download Code
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'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-07-11 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (235 votes), past polls