Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > parse question

Reply
Thread Tools

parse question

 
 
Pradeep
Guest
Posts: n/a
 
      05-14-2012
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:umper;
my @str="xxxx:yyyy:zzzz:aaaa::bbbb.9.5";
my @values;
foreach my $val (@str) {
@values = split('.', $val);
}
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      05-15-2012
In article
<(E-Mail Removed)>,
Pradeep <(E-Mail Removed)> 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:umper;
> 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/\..*//;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
noob question: easiest way to parse an int? user Java 21 07-20-2006 12:26 PM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
Re: A question to parse HTML files John Java 9 01-14-2005 09:47 AM
Re: parse data question Karl ASP .Net 1 08-26-2004 07:09 PM



Advertisments