develooper Front page | perl.beginners | Postings from April 2012

Re: Help parsing tab delimited files

Thread Previous | Thread Next
From:
Jim Gibson
Date:
April 8, 2012 22:39
Subject:
Re: Help parsing tab delimited files
Message ID:
p06240800cba82510493f@[192.168.1.3]
At 10:42 PM -0230 4/8/12, Tiago Hori wrote:
>Hi Guys,
>
>I know there are modules for parsing tab delimited files, but I am 
>trying to develop a script as a learning exercise to learn the 
>better, so any help would be appreciated.
>
>Let's say I have two columns and two rows:
>
>Joe \t Doe
>Jane \t Doe
>
>So here is what I got:
>
>#!usr/bin/perl
>use strict;
>my $name;
>my $lastname;
>my@array;
>open(FILE, "<", "columns.txt");
>while (<FILE>)
>{
>     @array = split (/\t/, $_);
>     print "$array[0]\n";
>     print "$array[1]\n";
>}
>close(FILE);
>
>So right now this prints Joe and Jane. It seems the split is putting 
>a column in the array. Is there any way that I could parse a row at 
>a time, with each element becoming a entry in the array? My goal is 
>to be able to go through each row at a time and find a specific 
>value.

If you want to save the data in an array for later processing, you 
can use an array-of-arrays. Do something like this:

my@array;
while (<FILE>) {
     push( @array, [ split (/\t/);
}

The first record will be saved as $array[0][0] = "Joe" and 
$array[0][1] = "Doe", and so on.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About