parse question

P

Pradeep

Hi,
I am a new bie in perl. I have a string as follows

xxxx:yyyy:zzzz:aaaa::bbbb.9.5

I need to discard the 9.5 and my resultant string should be
xxxx:yyyy:zzzz:aaaa::bbbb with .9.5 not be present.

I tried to use split as follows. But its not working as expected. I
will appreciate any help.

use Data::Dumper;
my @str="xxxx:yyyy:zzzz:aaaa::bbbb.9.5";
my @values;
foreach my $val (@str) {
@values = split('.', $val);
}
 
J

Jim Gibson

Pradeep said:
Hi,
I am a new bie in perl. I have a string as follows

xxxx:yyyy:zzzz:aaaa::bbbb.9.5

I need to discard the 9.5 and my resultant string should be
xxxx:yyyy:zzzz:aaaa::bbbb with .9.5 not be present.

I tried to use split as follows. But its not working as expected. I
will appreciate any help.

use Data::Dumper;
my @str="xxxx:yyyy:zzzz:aaaa::bbbb.9.5";

You should not be assigning a string to an array variable:

my $str = "xxxx:yyyy:zzzz:aaaa::bbbb.9.5";
my @values;
foreach my $val (@str) {
@values = split('.', $val);
}

There is no need for a loop, and you need to escape the period to match
only a period:

my @values = split(/\./,$str);

Your desired string is now in $values[0].

You could also use a regular expression:

$str =~ s/\..*//;
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top