Question

I have a file which shall have lines like,

<tag host="xyz|abc" some info />
<tag host="ijk,cdf" some info />

I'm getting the information of the host by using pattern matching and I'd like to split the value of the host. For some reason the following code doesn't seem to work for comma even though it looks correct.

if($line =~ m/(\s\S)*host=\"(\S+)\"(\s\S)*/)
{
($val) = ($2);
$val=~tr/!$()//ds;
my @values;
if($val =~ m/((\S+)\|(\S+))*/ )
{
    @values=split('\|',$val);
}
else
{
@values=split(',',$val);
}
#Perform some action on Values.
}

Can any one help me with this ? Thanks in advance.

Was it helpful?

Solution 2

No need of if-else in your code. You can combine the conditions into one line -code.

Change your code like this :

use strict;
use warnings;

if($line =~ m/(\s\S)*host=\"(\S+)\"(\s\S)*/)
{
($val) = ($2);
$val=~tr/!$()//ds;
my @values;
@values = split (/[,|]/,$val);
#Perform some action on Values.
}

OTHER TIPS

A few ideas:

  • use strict, use warnings and some indentation would be nice :)

  • Where you wrote (\s\S) I presume you are familiar with JavaScript and meant the character class [\s\S]? There is no need to match the text preceding and following the parts you are interested in

  • The /s modifier on tr/// is superfluous in conjunction with /d

  • Far easier to just collect all substrings of characters that are neither pipe nor comma

Here is how I would have written it

use strict;
use warnings;

while (my $line = <DATA>) {
    if ($line =~ m/host="(\S+)"/) {
        (my $href = $1) =~ tr/!$()//d;
        my @values = $href =~ /[^,|]+/g;
        print "@values\n";
    }
}

__DATA__
<tag host="xyz|abc" some info />
<tag host="ijk,cdf" some info />

output

xyz abc
ijk cdf
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top