Home > Archive > PERL Beginners > January 2006 > File parsing
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ajey Kulkarni 2004-10-14, 3:56 pm |
| Hi,
I'm parsing a file line by line. The fine is a comma separated file.
I would like to search & replace or simply DELETE a column(field).
00000001 8/11/1988 Development Government/Defense
USA CSCI GROUND STATION UPGRADE
Military Ground Command/Control 0 0 0
Say the line above is separated by commas,and i would like to parse the
same.
I'm doing this using split().
Now that depending on the user input, i want to search/replace or
simply delete the field (or column).
What is the best way to this. (Fastest??).
Regards
-Ajey
| |
| JupiterHost.Net 2004-10-14, 3:56 pm |
|
> What is the best way to this. (Fastest??).
perldoc -f open
and search on search.cpan.org for CSV
Lee.M - JupiterHost.Net
| |
| William Black 2006-01-22, 7:03 pm |
| Hello,
I'm reading from a file. I'm trying to read in five lines at a time where
each line has a newline and then process the lines into separare variables.
For example,
Input File
-------------
Stevens,
Craig A Triangle Family Care PA
106-A Ridgeview Dr
Cary, NC
View Profile & Phone | Appointment Services 0.4
Once the lines are read in, I want to store 'Stevens' into a Lname variable,
'Graig' in a Fname variable, 'A Triangle Family Care PA' into BusName
variable, '106-A Ridgeview Dr' into a Address variable, 'Cary' into a City
variable, and 'NC' into a state variable. Could someone point me in the
right direction?
regards,
--
William Black
wjblack74@gmail.com
| |
| JupiterHost.Net 2006-01-22, 7:03 pm |
|
William Black wrote:
> Hello,
Hello William,
> I'm reading from a file. I'm trying to read in five lines at a time where
> each line has a newline and then process the lines into separare variables.
> For example,
>
> Input File
> -------------
> Stevens,
> Craig A Triangle Family Care PA
> 106-A Ridgeview Dr
> Cary, NC
> View Profile & Phone | Appointment Services 0.4
> Once the lines are read in, I want to store 'Stevens' into a Lname variable,
> 'Graig' in a Fname variable, 'A Triangle Family Care PA' into BusName
> variable, '106-A Ridgeview Dr' into a Address variable, 'Cary' into a City
> variable, and 'NC' into a state variable. Could someone point me in the
> right direction?
What have you tried? Please be sure to send the code you've tried
(use()ing strict and warnings in it) and the specific problem with that
code, as generally the list doesn't do your homework or job for you :)
I'd do something like this:
a) get all lines from file:
perldoc -f open
b) process them 4 at a time
(see natatime() in List::MoreUtils I beleive)
use strict;
use warnings;
my @all_lines = _get_all_lines_array();
my @four;
for my $line (@all_lines) {
if(@four < 4) {
push @four, $line;
}
else {
# now we have the four lines, have at it
@four = (); # empty it for the next four
}
}
Likely there's faster more memory efficient ones, maybe a map or use of
index numbers instead of the data:
for my $index (0 .. $#all_lines) {
if(@four < 4) {
push @four, $index;
}
else {
# now we have the four index's of the ones we want, have at it
@four = ();
}
}
| |
| Xicheng 2006-01-22, 7:03 pm |
| William Black wrote:
> Hello,
>
> I'm reading from a file. I'm trying to read in five lines at a time where
> each line has a newline and then process the lines into separare variables.
> For example,
>
> Input File
> -------------
> Stevens,
> Craig A Triangle Family Care PA
> 106-A Ridgeview Dr
> Cary, NC
> View Profile & Phone | Appointment Services 0.4
> Once the lines are read in, I want to store 'Stevens' into a Lname variable,
> 'Graig' in a Fname variable, 'A Triangle Family Care PA' into BusName
> variable, '106-A Ridgeview Dr' into a Address variable, 'Cary' into a City
> variable, and 'NC' into a state variable. Could someone point me in the
> right direction?
When using <FH> in scalar context, it read the file by one line. so you
may do something like this:
while (<FH> ) {
chomp;
my $Lname = $_; #read 1 line
my ($Fname, $Busname) = split' ',<FH>,2; #one line more
my $Address = <FH>; # one line more
my ($City,$State) = split/, /,<FH>; #one line more
#chomp the variables, and then do things on the above variables.
you take 4 lines from your file each loop....
}
Xicheng
| |
| John Doe 2006-01-22, 7:03 pm |
| William Black am Sonntag, 22. Januar 2006 15.33:
> Hello,
>
> I'm reading from a file. I'm trying to read in five lines at a time where
> each line has a newline and then process the lines into separare variables.
> For example,
>
> Input File
> -------------
> Stevens,
> Craig A Triangle Family Care PA
> 106-A Ridgeview Dr
> Cary, NC
> View Profile & Phone | Appointment Services 0.4
> Once the lines are read in, I want to store 'Stevens' into a Lname
> variable, 'Graig' in a Fname variable, 'A Triangle Family Care PA' into
> BusName variable, '106-A Ridgeview Dr' into a Address variable, 'Cary' into
> a City variable, and 'NC' into a state variable. Could someone point me in
> the right direction?
A great part of the direction is the answer by Shawn to your previous answer,
where you intended to read 4 lines at a time instead of 5.
In Shawn's answer are the two lines:
# process @lines
print Dumper \@lines;
The first tells you where to process the accumulated lines,
the second shows you the structure of @lines.
Now you want parts of the lines assigned to some variables.
For that, please read about the possibilities to get substrings from a
string:
perldoc -f split
or, for more complicated cases:
perldoc perlre
and the other manuals mentioned therein at the bottom.
hth,
joe
| |
| Luke Bakken 2006-01-24, 6:56 pm |
| William Black wrote:
> Hello,
>=20
> I'm reading from a file. I'm trying to read in five lines at a time
> where each line has a newline and then process the lines into
> separare variables. For example,
>=20
> Input File
> -------------
> Stevens,
> Craig A Triangle Family Care PA
> 106-A Ridgeview Dr
> Cary, NC
> View Profile & Phone | Appointment Services 0.4
> Once the lines are read in, I want to store 'Stevens' into a Lname
> variable, 'Graig' in a Fname variable, 'A Triangle Family Care PA'
> into BusName variable, '106-A Ridgeview Dr' into a Address variable,
> 'Cary' into a City variable, and 'NC' into a state variable. Could
> someone point me in the right direction?
>=20
> regards,
Use Tie::File:
use strict;
use warnings;
use Tie::File;
my @f;
my $fname =3D shift;
tie @f, 'Tie::File', $fname or die "Can't tie file $fname: $^E";
for (my $i =3D 0; $i < @f; $i +=3D 5)
{
my ($lname, $fname, $busName, $address, $city, $state);
$f[$i] =3D~ /^([a-zA-Z]+),/;
$lname =3D $1;
$f[$i+1] =3D~ /^([a-zA-Z]+)\s(.*)$/;
$fname =3D $1;
$busName =3D $2;
chomp($address =3D $f[$i+2]);
($city, $state) =3D split /,/, $f[$i+3];
$state =3D~ s/\s+//g;
}
untie @f;
|
|
|
|
|