CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
processing a text file

 



rpp
New User

Apr 3, 2014, 5:49 AM

Post #1 of 3 (2199 views)
processing a text file Can't Post

I have a tab separated text file which looks like
row_id column_id value
2 4 3
11 33 1
121 2 5

I want to write it in a matrix format
................. column_id
.
.
.
.
row_id .................. value

(This post was edited by rpp on Apr 3, 2014, 5:51 AM)


BillKSmith
Veteran

Apr 3, 2014, 7:57 AM

Post #2 of 3 (2195 views)
Re: [rpp] processing a text file [In reply to] Can't Post

The following code does what you ask. Note: For clarity, I have used a pipe (|) rather then a TAB as a field separator character.


Code
use strict; 
use warnings;
use Data::Dumper;
my $SEP = qr/\|/;
my @matrix;

while (<DATA>) {
my( $row_id, $column_id, $value) = split /$SEP/;
$matrix[$column_id] = [$row_id, $value];
}

print Dumper \@matrix;
__DATA__;
2|4|3|
11|33|1|
121|2|5|


OUTPUT:

Code
$VAR1 = [ 
[
'
',
undef
],
undef,
[
'121',
'5'
],
undef,
[
'2',
'3'
],
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
[
'11',
'1'
]
];



The resulting matrix is very sparse. You should consider using a hash instead.
Good Luck,
Bill


rpp
New User

Apr 3, 2014, 9:32 AM

Post #3 of 3 (2191 views)
Re: [BillKSmith] processing a text file [In reply to] Can't Post

thank you for your answer

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives