> I have a database file, some_sch.txt that is arranged like this:
>
> username1
> First Last
> Some Community Sch
> 1000 S Adams St
> Town, Indiana 12345 U.S.A.
>
> username2
> First Last
> Some Community Sch
> 1000 S Adams St
> Town, Indiana 12345 U.S.A.
>
> username3
> First Last
> Some Community Sch
> 1000 S Adams St
> Town, Indiana 12345 U.S.A.
>
> I want the file to look like this:
>
> username1    First Last    sch_name     1000 S Adams St    Town,Indiana
> 12345 U.S.A.
> username2    First Last    sch_name     1000 S Adams St    Town,Indiana
> 12345 U.S.A.
> username3    First Last    sch_name     1000 S Adams St    Town,Indiana
> 12345 U.S.A.
>
> I don't know how to create a loop that will assign each scalar variable
> to the next line (field).  Here is what I have so far....  Any suggestions
> or hints, would be greatly appreciated!
>
> Thanks!
>
> #!/usr/bin/perl -w
>
> use strict;
>
>
> my $datafile = "C:/brian/marion_sch.txt";
> open (IN, $datafile);
> while (<IN>) {
>     chomp;
>
>     my ($user_name, $full_name, $cust_name, $address, $cszc, $blank_line)
=
>     (split // );


You won't get what you expected, since the first read is 'username1', the
second
read is 'First Last', etc etc. So you are unable to split in this way.


May be you can do in this way :

use strict;
open IN, "file.txt";
my ( @data, @result );

while (my $ln = <IN>)
{    chomp $ln;
        if (@data == 4 ) # From 'username' to  '1000 S Adams St'
            { push @result, (join "\t", @data);
                push @result, $_ ;     # This is '12345 U.S.A.'
                <IN> ;     # read the blank line and let it be passed
                @data = ()   # clear @data for the next user
            }
        push @data, $ln;
}

print "$_\n" for @result;

Note the code not tested, but guess the flow would be like this. Try modify
it
if you want it be in write-format ways.

HTH,
Bee



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