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. :-)
> 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
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!
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
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.
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)
just FYI - it gets easier. if your addresses are stored in addr.txt:
$ perl -ln00e 'print join ",", map qq["$_"], split /\n/' addr.txt
-jp
I think you just broke my brain.
Welcome to Perl. :-)
(see perldoc perlrun for a description of the -l, -n, and -e command
line arguments)
Paul Lalli
...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.
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
Only a little. :-)
perl -ln00e "print join \",\", map qq[\"$_\"], split /\n/" testing.txt
Thanks again for all your help, folks. :-)