Gossamer Forum
Home : General : Perl Programming :

Extracting data from line and the next

Quote Reply
Extracting data from line and the next
Hi

I am confused about how to go about writing this program in perl.

I have a text file with data in columns 8 and 9. I need to extract the data value from column 8 from the line being read and then extract the data value in column 9 from the next line. This needs to be repeated for all lines in the file. So, i would be taking data from column 8 from lines 1,3,5,7,ect and column 9 from lines 2,4,6,8..... I need to move these data values into an array.

Thanks for all your help

Nxstar
Quote Reply
Re: [nxstar] Extracting data from line and the next In reply to
Hi,

You don't really give us that much info ;)

What format is the data in? This example should work - but it assumes you are using tab delimiter:

Code:
my $i= 0;
my @loop;
open(READIT, "./data.txt");
while (<READIT>) {
$i++;
my @tmp = split /\t/, $_;
if ($i % 2) { push @loop, $tmp[8] } else { push @loop, $tmp[7] }
}
close(READIT);

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates

Last edited by:

Andy: Feb 9, 2009, 2:58 AM
Quote Reply
Re: [Andy] Extracting data from line and the next In reply to
I'm not quite sure if I misunderstood the question, but I think the condition should be
Code:
if ($i % 2) { push @loop, $tmp[7] } else { push @loop, $tmp[8] }
Quote Reply
Re: [neves] Extracting data from line and the next In reply to
Ooops, good point (being a %, instead of /)

I'll update my code above.

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] Extracting data from line and the next In reply to
cheers mate..
that was quite helpful