converting text data

V

Vumani Dlamini

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
 
D

DOV LEVENGLICK

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
 
N

nobull

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.
 
V

Vumani Dlamini

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,087
Latest member
JeremyMedl

Latest Threads

Top