Daniel Liston wrote:
Here is a tool I use to unfold long lines in LDAP outputs. It also works on mailboxes that have the Received: lines or Content-*: lines folded.
There are two things in your code that may cause a problem.
#!/usr/bin/perl
#syntax: unfold.pl filename > newfilename
if ($ARGV[0]) { local $/ = ''; open(FILE, "<$ARGV[0]") or die
"can't open $ARGV[0]: $!\n"; @records = <FILE>; close(FILE); } foreach $record (@records) { $record =~ s/\n\s//g; print
"$record\n\n"; }
You are removing a newline and a single whitespace character. If your input looks like this:
one two three
In the case of "(Received|Content-.*):" headers, I simply replace my substitution with s/\n\s/ /g, as folding is accomplished between words and prefixing continuation lines with a tab character.
Your substitution will result in:
one two three
And if your input looks like this:
one two three
Your substitution will result in:
onetwothree
In LDAP extracts, long lines are not split at the end of a word, so this is actually what I want to do here. I have to be able to reconnect words without putting a space back in.
However if you change the substitution it will screw up the record separator. You should chomp the records when you read from the file
and substitute all whitespace after the newline with a single space.
#!/usr/bin/perl
#syntax: unfold.pl filename > newfilename
@ARGV or die "usage: $0 filename\n";
my @records = do { local ( $/, *FILE ) = ''; open FILE, $ARGV[0] or die "can't open $ARGV[0]: $!"; grep chomp, <FILE>; } foreach my $record ( @records ) { $record =~ s/\n\s+/ /g; print "$record\n\n"; }
Or you could pare that down a bit. :-)
#!/usr/bin/perl
#syntax: unfold.pl filename > newfilename
@ARGV or die "usage: $0 filename\n";
$/ = ''; print for grep [ chomp, s/\n\s+/ /g, s/\z/\n\n/ ], <>;
I like that line. I will have to experiment with it.
[snip]
The second paragraph could be modified to fit your needs rather simply.
$count = 0; foreach $record (@records) { @recordnumber$count = $record;
^^^^^^^^^^^^^^^^^^^
Thanks. I should have tried that before posting
The intent was to make hashes (named on the fly) of the lines in each record.
Example, @recordnumber0 = "AAAAAAAA\nAAAAAAAAAA\nAAAAAAA" @recordnumber1 = "BBBBBBBB\nBBBBBBBBBB\nBBBBBBB" @recordnumber2 = "CCCCCCCC\nCCCCCCCCCC\nCCCCCCC"
$ perl -le'$record = 234; @recordnumber$count = $record;' Scalar
found where operator expected at -e line 1, near "@recordnumber$count" (Missing operator before $count?) syntax error
at -e line 1, near "@recordnumber$count " Execution of -e aborted due
to compilation errors.
$ perl -le '$record = "AAA\nAAA\nAAA"; @recordnumber{$count}= $record;'
Wrapping $count in {} braces gets past the missing operator error.
$count++ }
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]