473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

converting text data

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
Jul 19 '05 #1
3 2640
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
Jul 19 '05 #2
dv*****@hotmail.com (Vumani Dlamini) wrote in message news:<4b**************************@posting.google. com>...
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.
Jul 19 '05 #3
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

no****@mail.com wrote in message news:<4d**************************@posting.google. com>...
dv*****@hotmail.com (Vumani Dlamini) wrote in message news:<4b**************************@posting.google. com>...
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.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Newbee Adam | last post by:
I have been reading in help how I need to use decimal becuase currency does not exist like I used in vb6. I had a difficult time on google and msdn finding how or if I can take the value of text...
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
1
by: luna | last post by:
got so far then it broke and i cant get it working again - it was updating fine but not inserting and now im getting a "Error converting data type varchar to numeric" which i didnt have before.......
1
by: Ramakrishnan Nagarajan | last post by:
Hi, I am converting Excel data into a Dataset in C#. There are around 24 columns in the Excel Sheet. First I tried to insert one row with correct values in the Excel sheet. i.e. for text columns...
1
by: UKuser | last post by:
Hi Guys, I have a program which converts Excel spreadsheets to Javascript and allows interactivity. However it can't convert it to PHP, which is obviously better for users to view (in case J/S...
3
by: Sharon | last post by:
I have a buffer of byte that contains a raw data of a 1 byte-per-pixel image data. I need to convert this buffer to a Bitmap of Format32bppArgb and to a Bitmap of Format24bppRgb. Can anybody...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
4
by: Bob Alston | last post by:
Anyone have experience with converting an access app from Jet database to Mysql? I am specifically looking for any changes I would have to make to my access forms, queries, modules, vba code, etc....
25
by: Blasting Cap | last post by:
I keep getting errors that pop up when I am trying to convert an application from dotnet framework 1.1 to framework 2.0. The old project was saved in sourcesafe from Visual Studio 2003, and I have...
0
Krishna Ladwa
by: Krishna Ladwa | last post by:
In Sql Server 2000 Version, I found that no Notification message box appears when converting text column to varchar but the data gets truncated to the given size for the varchar. Whereas it appears...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.