K
kz
Hi Gurus,
Given the following text file (proprietary database format, thus no CPAN
modules available)
0001 08 01 24 22 24 25 22 64
0002 06 09 42 22 f3 8f
where
aaaa bb cc dd ee ff gg hh ii jj ....
aaaa: sequence number (hex, starting from 0001)
bb: length of record including this byte
cc: record type (01..2a)
bytes dd to EOL are the data. Each record type requires a specific amount of
data bytes (bb-2) and needs to be parsed using different criteria, therefore
I decided to code for each record type a different sub named type_01 thru
type_2a (#$% load of work, though...)
After consulting the perldsc manpage I came up with below code snippet,
which does exactly what needed, though I'm not confident if this is the best
way to do it.
#!/usr/bin/perl
use strict;
use warnings;
my %DATABASE;
my $dbfile = $ARGV[0];
open ( README, "<$dbfile") || print "ERROR: unable to open log file, error:
$! \n", exit 1;
# read data
while (my $line = <README>) {
chomp $line;
if ($line =~ /^(\w{4})\s+\w{2}\s+\w{2}\s+(\w{2})\s+(\w{2})\s?(.*)$/)
{
$DATABASE{$1}{length} = $2;
$DATABASE{$1}{recordtype} = $3;
$DATABASE{$1}{records} = [split /\s/,$4];
$DATABASE{$1}{parse} = \&{"type_".$3};
# I'm surprised that above line works at all....
} }
close ( README );
# process data
foreach my $key (sort keys %DATABASE) {
$DATABASE{$key}{parse}->($DATABASE{$key});
# I'm surprised that above line works at all....
}
exit 0;
sub type_01 {
print "type 01\n";
my $hh = $_[0];
print $hh->{recordtype},"\n";
print $hh->{length},"\n";
print join ("-", @{$hh->{records}}),"\n";
# further processing
}
....
sub type_09 {
print "type 09\n";
# further processing
}
.......
sub type_2a {
print "type 2a\n";
# further processing
}
Critics and suggestions are welcome.
Thanks in advance,
Zoltan Kandi, M. Sc.
Given the following text file (proprietary database format, thus no CPAN
modules available)
0001 08 01 24 22 24 25 22 64
0002 06 09 42 22 f3 8f
where
aaaa bb cc dd ee ff gg hh ii jj ....
aaaa: sequence number (hex, starting from 0001)
bb: length of record including this byte
cc: record type (01..2a)
bytes dd to EOL are the data. Each record type requires a specific amount of
data bytes (bb-2) and needs to be parsed using different criteria, therefore
I decided to code for each record type a different sub named type_01 thru
type_2a (#$% load of work, though...)
After consulting the perldsc manpage I came up with below code snippet,
which does exactly what needed, though I'm not confident if this is the best
way to do it.
#!/usr/bin/perl
use strict;
use warnings;
my %DATABASE;
my $dbfile = $ARGV[0];
open ( README, "<$dbfile") || print "ERROR: unable to open log file, error:
$! \n", exit 1;
# read data
while (my $line = <README>) {
chomp $line;
if ($line =~ /^(\w{4})\s+\w{2}\s+\w{2}\s+(\w{2})\s+(\w{2})\s?(.*)$/)
{
$DATABASE{$1}{length} = $2;
$DATABASE{$1}{recordtype} = $3;
$DATABASE{$1}{records} = [split /\s/,$4];
$DATABASE{$1}{parse} = \&{"type_".$3};
# I'm surprised that above line works at all....
} }
close ( README );
# process data
foreach my $key (sort keys %DATABASE) {
$DATABASE{$key}{parse}->($DATABASE{$key});
# I'm surprised that above line works at all....
}
exit 0;
sub type_01 {
print "type 01\n";
my $hh = $_[0];
print $hh->{recordtype},"\n";
print $hh->{length},"\n";
print join ("-", @{$hh->{records}}),"\n";
# further processing
}
....
sub type_09 {
print "type 09\n";
# further processing
}
.......
sub type_2a {
print "type 2a\n";
# further processing
}
Critics and suggestions are welcome.
Thanks in advance,
Zoltan Kandi, M. Sc.