Reducing amount of repetative code

B

Brad

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
 
A

Arndt Jonasson

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.)
 
J

Josef Moellers

Brad said:
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.
 
A

Anno Siegel

Brad said:
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
 
A

Anno Siegel

Brad said:
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
 
T

Tad McClellan

Brad said:
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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top