converting text data

Discussion in 'Perl' started by Vumani Dlamini, Oct 29, 2003.

  1. I have data which is in this format:
    ### data ###
    area=1101
    home=003
    mzer=00020
    mzec=101
    pmpr=00000
    pmpc=102
    bnsr=00000
    bnsc=103
    potr=00100
    potc=104
    swtr=00000
    ### end ####

    and would like to produce the following data

    area|home|amount|code
    1101,003,00020,101
    1101,003,00100,104

    In short I would like to drop codes with a zero amount.

    I am able to read in the file and produce the data, zero's included
    using this code, but am unable to drop the lines corresponding to zero
    amounts;
    ### code ####
    my ($area , $home, $amount);
    while (<DATA>){
    if (/area=(\d+)/) {
    $area = $1;
    }
    elsif (/home=(\d+)/) {
    $home = $1;
    }
    elsif (/(\S+)r=(\d+)/) {
    $home = $2;
    }
    elsif (/(\S+)c=(\d+)/) {
    print <OUTFILE> "$area,$home,$amount,$2\"
    }
    }

    I would also like to have my PERL programs in another directory rather
    than in the one where the <DATA> file is. I also need help on how I
    can change directory within the PERL program.

    Thanks a lot.



    Vumani
    Vumani Dlamini, Oct 29, 2003
    #1
    1. Advertising

  2. i am not sure where you are giving $ammount a value. however, if you
    want to filter out zero values you can try reading the file in to an array:
    @arr = (<DATA>)
    and then filtering that:
    @arr = grep /\w+=0+$/, @arr

    Vumani Dlamini wrote:

    >I have data which is in this format:
    >### data ###
    >area=1101
    >home=003
    >mzer=00020
    >mzec=101
    >pmpr=00000
    >pmpc=102
    >bnsr=00000
    >bnsc=103
    >potr=00100
    >potc=104
    >swtr=00000
    >### end ####
    >
    >and would like to produce the following data
    >
    >area|home|amount|code
    >1101,003,00020,101
    >1101,003,00100,104
    >
    >In short I would like to drop codes with a zero amount.
    >
    >I am able to read in the file and produce the data, zero's included
    >using this code, but am unable to drop the lines corresponding to zero
    >amounts;
    >### code ####
    >my ($area , $home, $amount);
    >while (<DATA>){
    > if (/area=(\d+)/) {
    > $area = $1;
    > }
    > elsif (/home=(\d+)/) {
    > $home = $1;
    > }
    > elsif (/(\S+)r=(\d+)/) {
    > $home = $2;
    > }
    > elsif (/(\S+)c=(\d+)/) {
    > print <OUTFILE> "$area,$home,$amount,$2\"
    > }
    >}
    >
    >I would also like to have my PERL programs in another directory rather
    >than in the one where the <DATA> file is. I also need help on how I
    >can change directory within the PERL program.
    >
    >Thanks a lot.
    >
    >
    >
    >Vumani
    >
    >


    --
    Regards,
    Dov Levenglick
    DOV LEVENGLICK, Oct 30, 2003
    #2
    1. Advertising

  3. Vumani Dlamini

    Guest

    (Vumani Dlamini) wrote in message news:<>...

    > I am able to read in the file and produce the data, zero's included
    > using this code,


    Not quite. There are many transcription errors in the code you've
    posted here. Please do not transcribe by hand, cut and paste your
    actual code. For this and much other helpful advice see the posting
    guidelines in comp.lang.perl.misc.

    > if (/area=(\d+)/) {
    > $area = $1;


    Not that it actually matters in this case but you probably should
    anchor your regualar expressions.

    > but am unable to drop the lines corresponding to zero amounts;


    > print <OUTFILE> "$area,$home,$amount,$2\"


    Make that

    print OUTFILE "$area,$home,$amount,$2\n"
    unless $ammount =~ /^0+$/;

    > I would also like to have my PERL programs in another directory rather
    > than in the one where the <DATA> file is.


    The special 'DATA' file is part of your Perl script so it can't be in
    a different directory. Or are you re-opening the DATA filehandle onto
    another file?

    It's Perl not PERL (see FAQ).

    There is nothing preventing you opening files in directories other
    than the current working directory using absolute or relative file
    paths.

    The current working directory is not in general the same as the
    directory containing your Perl script.

    > I also need help on how I can change directory within the PERL program.


    There is a Perl built in function to change the current working
    directory.
    Finding it in the list of Perl built-in functions (perlfunc) is left
    as a character-building exercise for the reader.

    Of course the change of current directory only affects the Perl
    process and any child processes it subsequently starts. It does not
    affect the parent process (e.g. the command shell from which the
    script was run). For further discussion see FAQ.

    This newsgroup does not exist (see FAQ). Please do not start threads
    here.
    , Oct 30, 2003
    #3
  4. Thanks a lot for your mails and for pointing out that its not PERL but
    Perl or perl (had no idea). Used cut and paste to submit here, thus
    the errors in the code but it works.

    Thanks for your help, "unless" did the trick in extracting amounts
    above zero.

    Vumani



    wrote in message news:<>...
    > (Vumani Dlamini) wrote in message news:<>...
    >
    > > I am able to read in the file and produce the data, zero's included
    > > using this code,

    >
    > Not quite. There are many transcription errors in the code you've
    > posted here. Please do not transcribe by hand, cut and paste your
    > actual code. For this and much other helpful advice see the posting
    > guidelines in comp.lang.perl.misc.
    >
    > > if (/area=(\d+)/) {
    > > $area = $1;

    >
    > Not that it actually matters in this case but you probably should
    > anchor your regualar expressions.
    >
    > > but am unable to drop the lines corresponding to zero amounts;

    >
    > > print <OUTFILE> "$area,$home,$amount,$2\"

    >
    > Make that
    >
    > print OUTFILE "$area,$home,$amount,$2\n"
    > unless $ammount =~ /^0+$/;
    >
    > > I would also like to have my PERL programs in another directory rather
    > > than in the one where the <DATA> file is.

    >
    > The special 'DATA' file is part of your Perl script so it can't be in
    > a different directory. Or are you re-opening the DATA filehandle onto
    > another file?
    >
    > It's Perl not PERL (see FAQ).
    >
    > There is nothing preventing you opening files in directories other
    > than the current working directory using absolute or relative file
    > paths.
    >
    > The current working directory is not in general the same as the
    > directory containing your Perl script.
    >
    > > I also need help on how I can change directory within the PERL program.

    >
    > There is a Perl built in function to change the current working
    > directory.
    > Finding it in the list of Perl built-in functions (perlfunc) is left
    > as a character-building exercise for the reader.
    >
    > Of course the change of current directory only affects the Perl
    > process and any child processes it subsequently starts. It does not
    > affect the parent process (e.g. the command shell from which the
    > script was run). For further discussion see FAQ.
    >
    > This newsgroup does not exist (see FAQ). Please do not start threads
    > here.
    Vumani Dlamini, Oct 31, 2003
    #4
    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. Alfredo Agosti
    Replies:
    3
    Views:
    346
    Aaron Bertrand - MVP
    Sep 19, 2003
  2. Elijah Odumosu
    Replies:
    1
    Views:
    186
    Roger Pack
    Apr 29, 2008
  3. Vumani Dlamini

    converting text data

    Vumani Dlamini, Oct 29, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    104
    Vumani Dlamini
    Oct 30, 2003
  4. Mike Green

    converting vertical text to horizontal text

    Mike Green, Feb 1, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    385
    Arndt Jonasson
    Feb 2, 2005
  5. Replies:
    2
    Views:
    163
    Tad McClellan
    Mar 22, 2005
Loading...

Share This Page