Extracting records

Discussion in 'Perl' started by Dj Frenzy, Dec 2, 2003.

  1. Dj Frenzy

    Dj Frenzy Guest

    Hi, I am looking for a solution to this problem. I receive a list of
    records like this, in one continuous string. I want to separate each
    record and extract some of the data for each record. The records look
    like this:

    (=YEAR:1991;month:[JAN]client-NAME:[Ali-Baba-Basket-Emporium]AREA:[SouthEast]value:[ú1905]=)(=YEAR:1997;month:[dec]client-NAME:[Fletcher]AREA:[Scotland(North)]discount:[7%]value:[ú741]=)(=YEAR:2003;month:[MAR]client-NAME:[Porridge-dot-com]AREA:[N.Ireland]discount:[7%]value:[ú335]=)

    For example, in this record:
    (=YEAR:2003;month:[MAR]client-NAME:[Porridge-dot-com]AREA:[N.Ireland]discount:[7%]value:[ú335]=)

    I want to extract the year: "2003"
    month: "MAR"
    clientName: "Porridge-dot-com"
    area: "N.Ireland"
    discount (which is an optional field): "7"
    value: 335.

    An help in how to do this would be appreciated.
    Cheers,
    Dave
     
    Dj Frenzy, Dec 2, 2003
    #1
    1. Advertising

  2. Dj Frenzy wrote:
    > I receive a list of records like this, in one continuous string. I
    > want to separate each record and extract some of the data for each
    > record. The records look like this:
    >
    > (=YEAR:1991;month:[JAN]client-NAME:[Ali-Baba-Basket-Emporium]AREA:[SouthEast]value:[ú1905]=)(=YEAR:1997;month:[dec]client-NAME:[Fletcher]AREA:[Scotland(North)]discount:[7%]value:[ú741]=)(=YEAR:2003;month:[MAR]client-NAME:[Porridge-dot-com]AREA:[N.Ireland]discount:[7%]value:[ú335]=)
    >
    > For example, in this record:
    > (=YEAR:2003;month:[MAR]client-NAME:[Porridge-dot-com]AREA:[N.Ireland]discount:[7%]value:[ú335]=)
    >
    > I want to extract the year: "2003"
    > month: "MAR"
    > clientName: "Porridge-dot-com"
    > area: "N.Ireland"
    > discount (which is an optional field): "7"
    > value: 335.


    How about:

    my @records;

    for (split /\)\(/) {

    if (/(\d+) # year
    [^\[]+\[
    ([^\]]+) # month
    [^\[]+\[
    ([^\]]+) # clientName
    [^\[]+\[
    ([^\]]+) # area
    (?:
    \]discount:\[
    (\d+) # discount
    )?
    [^\[]+\[\D?
    (\d+) # value
    /x) {

    push @records, {
    year => $1,
    month => $2,
    clientName => $3,
    area => $4,
    discount => ($5 or 0),
    value => $6,
    }
    }
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 2, 2003
    #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. kunal
    Replies:
    0
    Views:
    488
    kunal
    Oct 15, 2005
  2. Luke Airig
    Replies:
    0
    Views:
    823
    Luke Airig
    Dec 31, 2003
  3. Aleander

    extracting records from a file

    Aleander, May 29, 2005, in forum: C++
    Replies:
    2
    Views:
    361
    Karl Heinz Buchegger
    May 30, 2005
  4. Dan

    Delete records or update records

    Dan, May 10, 2004, in forum: ASP General
    Replies:
    1
    Views:
    489
    Ray at
    May 10, 2004
  5. Replies:
    3
    Views:
    710
    Anthony Jones
    Nov 2, 2006
Loading...

Share This Page