How to parse a string?

Discussion in 'Perl Misc' started by snoopy_@excite.com, Mar 5, 2007.

  1. Guest

    If I have a string that looks like this:

    ]^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart

    And I want to count all the instances of "GE" how can I do this?

    If I perform a simple search with regular expressions I just get one
    instance, I want all. I tried to split on GE, then reduce total count
    by 1, but this was not reliable. Is there an easy way to do this?

    Thanks.
     
    , Mar 5, 2007
    #1
    1. Advertising

  2. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > If I have a string that looks like this:
    >
    > ]^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart
    >
    > And I want to count all the instances of "GE" how can I do this?
    >
    > If I perform a simple search with regular expressions I just get one
    > instance, I want all.


    That works if done correctly. Since you don't show what you have done,
    I don't know what you did wrong. Show your code.

    >I tried to split on GE, then reduce total count
    > by 1, but this was not reliable.


    Have you read the documentation about split(), and what it has to say
    about dropping trailing empty fields? And how the LIMIT argument can
    be used to keep all fields?

    > Is there an easy way to do this?


    Using split:

    $_ = ']^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart';
    -- ( my $count =()= split /GE/, $_, -1);

    The regex solution is similar but simpler. You can figure it out.

    Anno
     
    -berlin.de, Mar 5, 2007
    #2
    1. Advertising

  3. DJ Stunks Guest

    On Mar 5, 2:22 pm, "" <> wrote:
    > If I have a string that looks like this:
    >
    > ]^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart
    >
    > And I want to count all the instances of "GE" how can I do this?
    >
    > If I perform a simple search with regular expressions I just get one
    > instance, I want all. I tried to split on GE, then reduce total count
    > by 1, but this was not reliable. Is there an easy way to do this?
    >
    > Thanks.


    perldoc -q occurrences

    -jp
     
    DJ Stunks, Mar 5, 2007
    #3
  4. wrote:
    > If I have a string that looks like this:
    >
    > ]^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart
    >
    > And I want to count all the instances of "GE" how can I do this?
    >
    > If I perform a simple search with regular expressions I just get one
    > instance, I want all.


    That's strange. I just wrote a 2 line test program and it correctly printed
    3 when using a simple RE on your sample data.
    I have a few suspicions why your test program didn't print 3 but there is
    little point of confusing you with wild guesses, therefore I am afraid you
    will have to post your sample program after all.

    jue
     
    Jürgen Exner, Mar 5, 2007
    #4
  5. <> wrote:


    > And I want to count all the instances of "GE" how can I do this?



    If the string is in $_, then:

    my $count = s/GE/GE/g;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 6, 2007
    #5
    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. Vincent

    parse string

    Vincent, Dec 16, 2003, in forum: Perl
    Replies:
    2
    Views:
    2,834
    Gunnar Hjalmarsson
    Dec 16, 2003
  2. Kevin Joseph
    Replies:
    5
    Views:
    4,405
    Joe Smith
    Aug 13, 2004
  3. Henri
    Replies:
    2
    Views:
    331
    Henri
    Nov 29, 2004
  4. Replies:
    19
    Views:
    1,184
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

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

Share This Page