For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > December 2004 > Re: make this more efficient?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: make this more efficient?
Dave Weaver

2004-12-03, 8:55 am

On Wed, 01 Dec 2004 01:26:05 GMT, roach <joey01@cfl.rr.com> wrote:
> Is there a more efficient way to do the following code?


-- <snip> --

1. use strict & warnings to get the most help you can from perl.

2. why create a 2nd file just to split the input into 144-byte chunks
when you can use $/ to do that for you?

3. lots of if ... elsif statements suggest a lookup table.

Something like (untested) :

#!/usr/bin/perl
use strict;
use warnings;

my %levels = (
NNNRRRNRRNRRNNNNNNNNRRNNNWNNNNNNNNNNNNNN
NNNNNNNNNNRNN0NNRNNN => 8,
NNNNNWNWWWNNNNNWNNNNRNNNNWNNNNNNNNNNNNNN
NNNNNNNNNNRNN0NNRNNN => 7,
# etc..
);

{
local $/ = \144;

open my $fs, 'FS' or die "Can't open 'FS' : $!";
while ( <$fs> ) {
my $switches = substr( $_, 70, 60 );
my $level = $levels{ $switches } || 'NONE';
print "$_ $level\n";
}
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com