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

}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to