S
Sesa Woruban
Hiya,
I'm very new to Perl and my brain is dead. I'm trying to create a
simple programme that will extract the pertinent lines (only those
with a : in them) from a plain flat text file (that represents my
inbox) and write only those files to another text file. This is what
I've got so far:
use strict;
use warnings;
my $key;
my %hash;
my $infile = '/home/sesaworu/mail/sesaworuban.net/test/input'; #
store the file
my $outfile = '>/home/sesaworu/mail/sesaworuban.net/test/output.txt';
open (INFILE, $infile) or die "cannot open $infile: $!"; # opens the
file
open (OUTFILE, $outfile) or die "cannot open $outfile: $!"; # opens
the file
while()
{
chomp;
$key='',next if /^\s*$/;
if(/([\w\s]+).*)/){
$key=$1;
push @{$hash{$key}},$2;
}
else
{
push @{$hash{$key}},$_ if $key;
}
}
for(sort keys %hash)
{
print OUTFILE "$_ : ".join("\n",@{$hash{$_}})."\n";
}
How does that look?
Cheers
Sesa
I'm very new to Perl and my brain is dead. I'm trying to create a
simple programme that will extract the pertinent lines (only those
with a : in them) from a plain flat text file (that represents my
inbox) and write only those files to another text file. This is what
I've got so far:
use strict;
use warnings;
my $key;
my %hash;
my $infile = '/home/sesaworu/mail/sesaworuban.net/test/input'; #
store the file
my $outfile = '>/home/sesaworu/mail/sesaworuban.net/test/output.txt';
open (INFILE, $infile) or die "cannot open $infile: $!"; # opens the
file
open (OUTFILE, $outfile) or die "cannot open $outfile: $!"; # opens
the file
while()
{
chomp;
$key='',next if /^\s*$/;
if(/([\w\s]+).*)/){
$key=$1;
push @{$hash{$key}},$2;
}
else
{
push @{$hash{$key}},$_ if $key;
}
}
for(sort keys %hash)
{
print OUTFILE "$_ : ".join("\n",@{$hash{$_}})."\n";
}
How does that look?
Cheers
Sesa