Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Parsing an address list

3 views
Skip to first unread message

Sarek of Vulcan

unread,
Aug 11, 2006, 11:01:07 PM8/11/06
to
I have an ASCII list of addresses that I need to parse into something
more-friendly to the software I need to use it with. I usually use
Visual FoxPro, and can think of a few ways to do it there: it just
strikes me that perl probably has much easier ways to do it. :-)

The list looks something like this at the moment:
<bof>
NAME
TITLE
ADDR1
CITY, STATE ZIP

NAME
COMPANY
ADDR1
CITY STATE ZIP

NAME
COMPANY
ADDR1
ADDR2
CITY. STATE ZI P

NAME
TITLE
COMPANY
ADDR1
CITY STATE ZIP
<eof>

I'd like to turn this into a comma-delimited file with the fields going
across, as is more usual. Obviously, Perl can't help me with telling
the difference between TITLE, COMPANY, and ADDR1, but if I could break
it down into LINE1, LINE2, LINE3, etc., the postal software can
probably take it from there.

I'm not asking for code here: I'm just looking for a judgement on
whether this is as Perl-trivial as I think it should be. If it is, it
might be time to learn Perl and see if it takes care of this faster
than VFP does. :-)

Paul Lalli

unread,
Aug 11, 2006, 11:07:10 PM8/11/06
to
Sarek of Vulcan wrote:
> The list looks something like this at the moment:
> <bof>
> NAME
> TITLE
> ADDR1
> CITY, STATE ZIP
>
> NAME
> COMPANY
> ADDR1
> CITY STATE ZIP
>
> NAME
> COMPANY
> ADDR1
> ADDR2
> CITY. STATE ZI P
>
> NAME
> TITLE
> COMPANY
> ADDR1
> CITY STATE ZIP
> <eof>
>
> I'd like to turn this into a comma-delimited file with the fields going
> across, as is more usual

> I'm not asking for code here: I'm just looking for a judgement on


> whether this is as Perl-trivial as I think it should be.

Yes, it is trivial in Perl. Enable paragraph-mode (see: perldoc -q
paragraph), read the file one record at a time. Split the record on
the newline (see: perldoc -f split), print the resulting fields
separated by commas (see: perldoc -f join).

If you make an attempt in Perl and it doesn't quite work for you, post
a short-but-complete script that demonstrates the error, and we can
probably help you fix it.

Good luck,
Paul Lalli

Sarek of Vulcan

unread,
Aug 12, 2006, 12:46:18 AM8/12/06
to
Paul Lalli wrote:
> Yes, it is trivial in Perl. Enable paragraph-mode (see: perldoc -q
> paragraph), read the file one record at a time. Split the record on
> the newline (see: perldoc -f split), print the resulting fields
> separated by commas (see: perldoc -f join).

local $/ = "\n\n";
open(ADDRESSES, "c:\\temp\\testing.txt") or die "Could not open input
file: $! !";
open(OUTCSV, "> c:\\temp\\test2.txt") or die "Could not open output
file: $!";

while(<ADDRESSES>) {
$NextRec = "\"".join("\",\"", split("\n"))."\"\n";
print OUTCSV $NextRec;
}

Good grief, if I had realized it was that easy, I would have picked it
up long ago...

Thanks!

DJ Stunks

unread,
Aug 12, 2006, 10:22:57 AM8/12/06
to
Sarek of Vulcan wrote [at 11 Aug 2006 21:46:18 -0700]
> Paul Lalli wrote [at 11 Aug 2006 20:07:10 -0700]

Hang on a sec, you claim to have known no perl at all before this? But
gleaned enough information from Paul's tips to be able to whip out your
own perl script (complete with variable localization, and error
checking) in only an hour and a half? You really must be from
Vulcan....... :P

Anyway, for your future scripts:
1) use strict
2) use warnings
3) use forward slashes even on Win32:

my $outputfile = 'c:/temp/test2.txt';

4) use lexical filehandles and the three-element form of open:

open my $OUTCSV, '>', $outputfile or die...

5) use named lexicals rather than relying on $_:

while ( my $record = <$ADDRESSES> ) {

6) use single quotes, q// or qq// in order to not have to
escape embedded doublequotes:

my $next_rec = q{"} . join( q{","}, split "\n", $record) . qq{"\n};

> Thanks!

any time, Vulcan.

-jp

Sarek of Vulcan

unread,
Aug 13, 2006, 12:20:09 AM8/13/06
to
DJ Stunks wrote:
> > local $/ = "\n\n";
> > open(ADDRESSES, "c:\\temp\\testing.txt") or die "Could not open input
> > file: $! !";
> > open(OUTCSV, "> c:\\temp\\test2.txt") or die "Could not open output
> > file: $!";
> >
> > while(<ADDRESSES>) {
> > $NextRec = "\"".join("\",\"", split("\n"))."\"\n";
> > print OUTCSV $NextRec;
> > }
> >
> > Good grief, if I had realized it was that easy, I would have picked it
> > up long ago...
>
> Hang on a sec, you claim to have known no perl at all before this? But
> gleaned enough information from Paul's tips to be able to whip out your
> own perl script (complete with variable localization, and error
> checking) in only an hour and a half? You really must be from
> Vulcan....... :P

Heh. I've read through the llama book, but that was years ago, and I
never tried writing my own scripts. I knew how to concatenate from a
script I had to work with before, but for the rest -- the online docs
really _are_ that good, once I got the pointer on what to look for. :-)
Knowing that everything gets dumped into $_, the rest flows naturally.

Sarek of Vulcan

unread,
Aug 13, 2006, 12:22:15 AM8/13/06
to
DJ Stunks wrote:
> Anyway, for your future scripts:
> 1) use strict
> 2) use warnings
> 3) use forward slashes even on Win32:
>
> my $outputfile = 'c:/temp/test2.txt';
>
> 4) use lexical filehandles and the three-element form of open:
>
> open my $OUTCSV, '>', $outputfile or die...
>
> 5) use named lexicals rather than relying on $_:
>
> while ( my $record = <$ADDRESSES> ) {
>
> 6) use single quotes, q// or qq// in order to not have to
> escape embedded doublequotes:
>
> my $next_rec = q{"} . join( q{","}, split "\n", $record) . qq{"\n};

And thanks for the pointers. Perl Best Practices is high on my list of
books to obtain in the near future...

use...@davidfilmer.com

unread,
Aug 13, 2006, 3:32:14 AM8/13/06
to
Sarek of Vulcan wrote:
> And thanks for the pointers. Perl Best Practices is high on my list of
> books to obtain in the near future...

I've often said that PBP should be the second Perl book anyone ever
buys. Sometimes, someone will ask me what I think should be the first
book. I always say "it doesn't matter."

Many books will teach you Perl (which is why it doesn't really matter
which book you pick up first). But only PBP will teach you how to do
Perl properly. After you learn Perl, but before you write a lot of
Perl programs, you ought to learn good Perl programming habits. You
will NOT be sorry!

--
David Filmer (http://DavidFilmer.com)

DJ Stunks

unread,
Aug 13, 2006, 11:35:28 AM8/13/06
to

just FYI - it gets easier. if your addresses are stored in addr.txt:

$ perl -ln00e 'print join ",", map qq["$_"], split /\n/' addr.txt

-jp

Sarek of Vulcan

unread,
Aug 13, 2006, 10:26:14 PM8/13/06
to
DJ Stunks wrote:
> just FYI - it gets easier. if your addresses are stored in addr.txt:
>
> $ perl -ln00e 'print join ",", map qq["$_"], split /\n/' addr.txt

I think you just broke my brain.

Paul Lalli

unread,
Aug 14, 2006, 6:57:45 AM8/14/06
to

Welcome to Perl. :-)

(see perldoc perlrun for a description of the -l, -n, and -e command
line arguments)

Paul Lalli

Sarek of Vulcan

unread,
Aug 14, 2006, 3:36:03 PM8/14/06
to

...and my ActivePerl. :-)

c:\temp>perl -ln00e 'print join ",", map qq["$_"], split /\n/'
testing.txt
Can't find string terminator "'" anywhere before EOF at -e line 1.

Is the syntax off, or did I do something wrong when I copied it over?
Thanks again.

DJ Stunks

unread,
Aug 14, 2006, 3:41:26 PM8/14/06
to

that syntax was from *nix. check out

perldoc -q "Why don't Perl one-liners work on my DOS/Mac/VMS system?"

you'll need to adjust the " and ' marks. and (like everything
Windows?) it will also get a little uglier :P

-jp

Sarek of Vulcan

unread,
Aug 14, 2006, 3:55:41 PM8/14/06
to
DJ Stunks wrote:
> Sarek of Vulcan wrote:
> > Sarek of Vulcan wrote:
> > > DJ Stunks wrote:
> > > > just FYI - it gets easier. if your addresses are stored in addr.txt:
> > > >
> > > > $ perl -ln00e 'print join ",", map qq["$_"], split /\n/' addr.txt
> > >
> > > I think you just broke my brain.
> >
> > ...and my ActivePerl. :-)
>
> you'll need to adjust the " and ' marks. and (like everything
> Windows?) it will also get a little uglier :P

Only a little. :-)

perl -ln00e "print join \",\", map qq[\"$_\"], split /\n/" testing.txt

Thanks again for all your help, folks. :-)

0 new messages