Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

File parsing
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


Report this thread to moderator Post Follow-up to this message
Old Post
Ajey Kulkarni
10-14-04 08:56 PM


Re: File parsing

> What is the best way to this. (Fastest??).

perldoc -f open
and search on search.cpan.org for CSV

Lee.M - JupiterHost.Net

Report this thread to moderator Post Follow-up to this message
Old Post
JupiterHost.Net
10-14-04 08:56 PM


re: File Parsing
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


Report this thread to moderator Post Follow-up to this message
Old Post
William Black
01-23-06 12:03 AM


Re: File Parsing

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 = ();
}

}

Report this thread to moderator Post Follow-up to this message
Old Post
JupiterHost.Net
01-23-06 12:03 AM


Re: File Parsing
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 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


Report this thread to moderator Post Follow-up to this message
Old Post
Xicheng
01-23-06 12:03 AM


Re: File Parsing
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' 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

Report this thread to moderator Post Follow-up to this message
Old Post
John Doe
01-23-06 12:03 AM


RE: File Parsing
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;

Report this thread to moderator Post Follow-up to this message
Old Post
Luke Bakken
01-24-06 11:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:57 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.