Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Reducing amount of repetative code

Reply
Thread Tools

Reducing amount of repetative code

 
 
Brad
Guest
Posts: n/a
 
      10-27-2004
Hello,

I'm trying to reduce some code which repeats itself
in my program, but I can't find a clever way to do it.
I'm parsing a file which looks like this:

____
; Key1: Value1
; Key2: Value2 22:33
; Key3: Value3

and so on
____

The "\d\d:\d\d" with Key2 will always appear.

I'm parsing this file by using this loop:

_____
while (<>)
{
if (/^;\s+Key1: (.*)/)
{
$object->method('a string connect with Key1' => $1);
}

if (/^;\s+Key2: (.*) \d\d:\d\d/)
{
$object->method('a string connect with Key2' => $1);
}

if (/^;\s+Key3: (.*)/)
{
$object->method('a string connect with Key3' => $1);
}
}
_____


Each time I find a line which matches the regex I call a method
on an object (neither of which are relevant) with two arguments
based on the result of the match.

Can anyone think of a way to reduce the amount of code here?
An awful lot of it is repeated.


Thanks very much,
Brad
 
Reply With Quote
 
 
 
 
Arndt Jonasson
Guest
Posts: n/a
 
      10-27-2004

http://www.velocityreviews.com/forums/(E-Mail Removed) (Brad) writes:
>
> I'm trying to reduce some code which repeats itself
> in my program, but I can't find a clever way to do it.
> I'm parsing a file which looks like this:


One way which comes to mind is to write some code that generates
the stereotypical code. A good programming language to use for
program-generating programs is of course Perl.

(If you have any reason to modify the generated code by hand after
it's been generated, discrepancies will appear between the true source
and the modified generated code, but that's a subject for another
discussion forum.)

 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      10-27-2004
Brad wrote:
> Hello,
>
> I'm trying to reduce some code which repeats itself
> in my program, but I can't find a clever way to do it.
> I'm parsing a file which looks like this:
>
> ____
> ; Key1: Value1
> ; Key2: Value2 22:33
> ; Key3: Value3
>
> and so on
> ____
>
> The "\d\d:\d\d" with Key2 will always appear.
>
> I'm parsing this file by using this loop:
>
> _____
> while (<>)
> {
> if (/^;\s+Key1: (.*)/)
> {
> $object->method('a string connect with Key1' => $1);
> }
>
> if (/^;\s+Key2: (.*) \d\d:\d\d/)
> {
> $object->method('a string connect with Key2' => $1);
> }
>
> if (/^;\s+Key3: (.*)/)
> {
> $object->method('a string connect with Key3' => $1);
> }
> }
> _____
>
>
> Each time I find a line which matches the regex I call a method
> on an object (neither of which are relevant) with two arguments
> based on the result of the match.
>
> Can anyone think of a way to reduce the amount of code here?
> An awful lot of it is repeated.


Use an array of hashes:

my @table = (
{ Key => "Key1", Postfix => '' },
{ Key => "Key2", Postfix => ' \d\d:\d\d' },
{ Key => "Key3", Postfix => '' }
);
while (my $l = <>) {
foreach (@table) {
my $key = ${$_}{Key};
my $postfix = ${$_}{Postfix};
if ($l =~ m/^;\s+$key: (.*)$postfix/) {
$object->method("a string connect with $key" => $1);
last;
}
}
}

Not much less code, but little repetition and easily expandable.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-27-2004
Brad <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hello,
>
> I'm trying to reduce some code which repeats itself
> in my program, but I can't find a clever way to do it.
> I'm parsing a file which looks like this:
>
> ____
> ; Key1: Value1
> ; Key2: Value2 22:33
> ; Key3: Value3
>
> and so on
> ____
>
> The "\d\d:\d\d" with Key2 will always appear.


Then why try to match it?

> I'm parsing this file by using this loop:
>
> _____
> while (<>)
> {
> if (/^;\s+Key1: (.*)/)
> {
> $object->method('a string connect with Key1' => $1);
> }
>
> if (/^;\s+Key2: (.*) \d\d:\d\d/)
> {
> $object->method('a string connect with Key2' => $1);
> }
>
> if (/^;\s+Key3: (.*)/)
> {
> $object->method('a string connect with Key3' => $1);
> }
> }
> _____
>
>
> Each time I find a line which matches the regex I call a method
> on an object (neither of which are relevant) with two arguments
> based on the result of the match.
>
> Can anyone think of a way to reduce the amount of code here?
> An awful lot of it is repeated.


Sure. The price is a more complex regex, because it now has to
cover all the alternatives. Untested:

/^;\s+Key(\d): (.*?)(?: \d\d:\d\d)?$/ and
print "a string connected with Key$1 => $2\n" while <>;

Anno


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-27-2004
Brad <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hello,
>
> I'm trying to reduce some code which repeats itself
> in my program, but I can't find a clever way to do it.
> I'm parsing a file which looks like this:
>
> ____
> ; Key1: Value1
> ; Key2: Value2 22:33
> ; Key3: Value3
>
> and so on
> ____
>
> The "\d\d:\d\d" with Key2 will always appear.
>
> I'm parsing this file by using this loop:
>
> _____
> while (<>)
> {
> if (/^;\s+Key1: (.*)/)
> {
> $object->method('a string connect with Key1' => $1);
> }
>
> if (/^;\s+Key2: (.*) \d\d:\d\d/)
> {
> $object->method('a string connect with Key2' => $1);
> }
>
> if (/^;\s+Key3: (.*)/)
> {
> $object->method('a string connect with Key3' => $1);
> }
> }
> _____
>
>
> Each time I find a line which matches the regex I call a method
> on an object (neither of which are relevant) with two arguments
> based on the result of the match.
>
> Can anyone think of a way to reduce the amount of code here?
> An awful lot of it is repeated.


Sure. The price is a more complex regex, because it now has to
cover all the alternatives. Untested:

/^;\s+Key(\d): (.*?)(?: \d\d:\d\d)?$/ and
print "a string connected with Key$1 => $2\n" while <>;

Anno



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-27-2004
Brad <(E-Mail Removed)> wrote:

> I'm trying to reduce some code which repeats itself
> in my program, but I can't find a clever way to do it.



Look-ahead can be used for that.


> if (/^;\s+Key1: (.*)/)


> $object->method('a string connect with Key1' => $1);


> if (/^;\s+Key2: (.*) \d\d:\d\d/)


> if (/^;\s+Key3: (.*)/)




> Can anyone think of a way to reduce the amount of code here?



You can replace all 3 if's with this one (untested):

if ( /^;\s+(Key[123]): (.*?)(?=\d\d:\d\d|$)/ ) {
$object->method("a string connect with $1" => $2);
}


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Leigh's repetative mistakes Paul C++ 1 02-26-2011 08:15 PM
Reducing code of ResultSet DTOs Alex Java 4 04-07-2006 04:58 PM
Reducing Repeated code for arrays JellyBum C Programming 2 05-03-2004 04:12 PM
Python re repetative matching Rich Python 1 12-23-2003 01:38 AM
STL & reducing code bloat Salvador I. Ducros C++ 5 08-04-2003 11:20 PM



Advertisments