in reply to Parsing Issue
Regarding your line-by-line issue. I suspect something else is the cause of your messed-up output. (Specifics, please) But if the file is not too big, you can suck the whole thing up into a single scaler and then do the three s/// alterations on the whole thing at once.#!/usr/bin/perl -w use strict; sub parse { my $flatfile = shift; my %replace = ('<Tab>' => "\t", '<Numpad_3>' => "\n", 'pad_3' => "\n", 'etc' => "etc", ); my @delete = ('<Tab>', '<Up>', '<Down>', '<PgUp>', '<Num_', '<Num', '<Nu', 'etc', 'etc', ); #open(CLOG, "<$flatfile") # || die ("Cannot open $flatfile for reading... $!\n"); #foreach $tracknumber (<CLOG>) { foreach my $tracknumber (<DATA>) { for my $chr ('0'..'9', '-', '/', '.') { $tracknumber =~ s/<Num_($chr)>/$chr/g; } for (keys %replace) { $tracknumber =~ s/$_/$replace{$_}/g; } for (@delete) { $tracknumber =~ s/$_//g; } #open(OUTPUT, "+>>$output") # || die ("Cannot open output file$!\n"); #print OUTPUT "$tracknumber"; #close OUTPUT; print "$tracknumber"; } #close CLOG; } parse('somefile'); __DATA__ Since I have no idea what keylogger data looks <Tab>like,<Up> I'll just <PgUp> have to fudge <Tab>up<Down> something <Num_4> a test<Num_.>
Update: After sleeping on it, I repent of suggesting the use of a hash to store the %replace data. It works in this case but it's a risky pattern. The order that keys are returned from a hash is not guaranteed. So consider the @delete data above and imagine if 'Num_', 'Num', and 'Nu' were replace items. If the data at hand were 'blah blah Num_ blah', it would make a difference if 'Nu' were evaluated before the other two. So we need to store these Replace items in an array to assure they will be evaluated in the specified order. It could be an array of hashes but there's no need for that, an array of arrays will do nicely and is more efficient. (untested snippets follow)
my @replace = (['<Tab>', "\t"], ['<Numpad_3>', "\n"], ['pad_3', "\n"], ['etc', "etc"]); # ... later in loop foreach my $aref (@replace) { $tracknumber =~ s/$aref->[0]/$aref->[1]/g; }
------------------------------------------------------------
"Perl is a mess
and that's good because the
problem space is also a mess." - Larry Wall
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Parsing Issue
by alkaloid (Initiate) on Feb 06, 2002 at 19:49 UTC | |
by dvergin (Monsignor) on Feb 07, 2002 at 04:31 UTC |