parse question

Discussion in 'Perl Misc' started by Pradeep, May 14, 2012.

  1. Pradeep

    Pradeep Guest

    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);
    }
     
    Pradeep, May 14, 2012
    #1
    1. Advertising

  2. Pradeep

    Jim Gibson Guest

    In article
    <>,
    Pradeep <> 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/\..*//;
     
    Jim Gibson, May 15, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl

    Re: parse data question

    Karl, Aug 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    393
  2. John
    Replies:
    9
    Views:
    404
    Andrew Thompson
    Jan 14, 2005
  3. user
    Replies:
    21
    Views:
    755
    Lasse Reichstein Nielsen
    Jul 20, 2006
  4. Replies:
    19
    Views:
    1,189
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    216
    7stud --
    Feb 20, 2008
Loading...

Share This Page