Home: Perl Programming Help: Beginner:
string manipulation



mumshquat
New User

Jan 29, 2011, 1:47 PM


Views: 468
string manipulation

Sorry if this is a re-post, but I've spent an hour searching through the forums, and haven't found what I'm looking for yet. I am extracting information from a file that has a big ugly table in it that looks something like:

15.0000p 39.570m 2 + + + + + + + +
16.0000p 44.642m 2 + + + + + + + +
17.0000p 49.713m ba + + + + + + + +
18.0000p 54.785m ba + + + + + + + +
19.0000p 59.856m ba + + + + + + + +
20.0000p 64.928m-ba-----+------+------+------+------+------+------+------+-

for about 60k entries or more. I am trying to get the data from the first 2 columns, but as you can see on the line that starts with 20.0000p the second column can get blended in with the stuff going on on the right side. I am unable to figure out a good method for ignoring the characters a,b,-,and+ when I extract that information.

Can anyone point me in the right direction?


FishMonger
Veteran / Moderator

Jan 29, 2011, 3:00 PM


Views: 461
Re: [mumshquat] string manipulation


Code
#!/usr/bin/perl 

use strict;
use warnings;

while( my $line = <DATA> ) {
my $fld1 = substr($line, 0, 8);
my $fld2 = substr($line, 9, 7);

print "$fld1,$fld2\n";
}

__DATA__
15.0000p 39.570m 2 + + + + + + + +
16.0000p 44.642m 2 + + + + + + + +
17.0000p 49.713m ba + + + + + + + +
18.0000p 54.785m ba + + + + + + + +
19.0000p 59.856m ba + + + + + + + +
20.0000p 64.928m-ba-----+------+------+------+------+------+------+------+-



(This post was edited by FishMonger on Jan 29, 2011, 3:01 PM)