Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this message> What is the best way to this. (Fastest??). perldoc -f open and search on search.cpan.org for CSV Lee.M - JupiterHost.Net
Post Follow-up to this messageHello, 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
Post Follow-up to this message
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 variabl
e,
> '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 = ();
}
}
Post Follow-up to this messageWilliam 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 variabl
e,
> '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
Post Follow-up to this messageWilliam 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' int o > a City variable, and 'NC' into a state variable. Could someone point me i n > 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
Post Follow-up to this messageWilliam 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;
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.