parse string

Discussion in 'Perl' started by Vincent, Dec 16, 2003.

  1. Vincent

    Vincent Guest

    hi,

    can anyone help ?

    $text=qq(a11.CLEC_ID ACNA,
    count(1) PR402RSSSDEN,
    sum(Decode(a11.CMPINTV-a11.APPINTV,0,a11.COMP_MAC_DY_CNT,a11.LEAST_DELAY_DAYS))
    PR402RSSSNUM,
    (STDDEV(Decode(CMPINTVa11.APPINTV,0,COMP_MAC_DY_CNT,LEAST_DELAY_DAYS))
    * 1.0) PR402RSSSSD)

    how can i parse this comma-separated string
    but should not split if comma is inside parentheses() ?

    thanks for help

    vincent
    Vincent, Dec 16, 2003
    #1
    1. Advertising

  2. Vincent wrote:
    >
    > $text=qq(a11.CLEC_ID ACNA,
    > count(1) PR402RSSSDEN,
    > sum(Decode(a11.CMPINTV-a11.APPINTV,0,a11.COMP_MAC_DY_CNT,a11.LEAST_DELAY_DAYS))
    > PR402RSSSNUM,
    > (STDDEV(Decode(CMPINTVa11.APPINTV,0,COMP_MAC_DY_CNT,LEAST_DELAY_DAYS))
    > * 1.0) PR402RSSSSD)
    >
    > how can i parse this comma-separated string
    > but should not split if comma is inside parentheses() ?


    One approach is to replace those commas you want to split on with some
    unique character, and then split:

    $text =~ s/(\([^\)]+\))|,\s*/$1 or "\0"/eg;
    my @text = split /\0/, $text;

    But don't forget to study the applicable Q/A in the Perl FAQ:

    perldoc -q "except when inside"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 16, 2003
    #2
    1. Advertising

  3. Gunnar Hjalmarsson wrote:
    > One approach is to replace those commas you want to split on with
    > some unique character, and then split:
    >
    > $text =~ s/(\([^\)]+\))|,\s*/$1 or "\0"/eg;
    > my @text = split /\0/, $text;


    It just struck me that this method isn't reliable, since there are
    nested parentheses. You'd better go for a module. (See the FAQ.)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 16, 2003
    #3
    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. Kevin Joseph
    Replies:
    5
    Views:
    4,330
    Joe Smith
    Aug 13, 2004
  2. Henri
    Replies:
    2
    Views:
    308
    Henri
    Nov 29, 2004
  3. Lelle
    Replies:
    1
    Views:
    459
    Patrice
    Mar 24, 2005
  4. Replies:
    19
    Views:
    1,100
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

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

Share This Page