Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

parse question

7 views
Skip to first unread message

Pradeep

unread,
May 14, 2012, 3:10:44 PM5/14/12
to
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);
}

Jim Gibson

unread,
May 14, 2012, 8:01:04 PM5/14/12
to
In article
<5aa28f5e-d8f5-4989...@em1g2000vbb.googlegroups.com>,
Pradeep <bubunia...@gmail.com> wrote:

> 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/\..*//;
0 new messages