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
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