Update multiple lines in a flat file from array

B

blnukem

Hi All

I looking for a simple way to update multiple lines in a flat file from
incoming array with multiple values the file structure goes like this:
Part-number | Name | Quantity

The file looks like this:

123456|headlamp|6
123457|camshaft|3
123458|tire|8
123459|wipperblades|10

The updated incoming values look like this
Part-number | NEW Quantity

123457 4
123459 8

So what I need it to do is update the flat file so it will look like this:

123456|headlamp|6
123457|camshaft|4
123458|tire|8
123459|wipperblades|8

I cant seem to make it work without getting multiple duplicate entry's

Thanks in advance
Blnukem
 
P

Paul Lalli

blnukem said:
I looking for a simple way to update multiple lines in a flat file from
incoming array with multiple values the file structure goes like this:
Part-number | Name | Quantity

The file looks like this:

123456|headlamp|6
123457|camshaft|3
123458|tire|8
123459|wipperblades|10

The updated incoming values look like this
Part-number | NEW Quantity

123457 4
123459 8

You said the new values are from "incoming array". The above doesn't
look anything like a Perl array. Whenever possible, speak Perl, not
English. Show us exactly from where and how your data is arriving.
So what I need it to do is update the flat file so it will look like this:

123456|headlamp|6
123457|camshaft|4
123458|tire|8
123459|wipperblades|8

I cant seem to make it work without getting multiple duplicate entry's

What exactly have you tried? Post your attempt, and we can help you see
where you went wrong.

Paul Lalli
 
B

blnukem

incoming array:

foreach $Pair (@Pairs) {
($UniqeID, $Quantity) = split(/=/, $Pair);
}
 
P

Paul Lalli

[top posting rearranged as it should be]
incoming array:

foreach $Pair (@Pairs) {
($UniqeID, $Quantity) = split(/=/, $Pair);
}

First, please don't top post. That means please post your reply below
what you are replying to.

Second, you haven't replied to the second part of my post. Please post
the code that you claim isn't working for you. We can't help you debug
your program if we can't see your code.

Paul Lalli
 
D

dan baker

blnukem said:
Hi All

I looking for a simple way to update multiple lines in a flat file from
incoming array with multiple values the file structure goes like this:
Part-number | Name | Quantity

The file looks like this:

123456|headlamp|6
-----------------------------------------

this looks like a pretty good candidate to use a tie()ed hash DBfile
rather than a flat text file read in and out.... Be a lot faster and
use less memory probably.

If you insist on a flat text file, and its not *huge* I guess you
could read it all into a hash in memory, do your thing, and then write
it all back out (overwriting the old file).

d
 
E

Eric Bohlman

(e-mail address removed) (dan baker) wrote in
this looks like a pretty good candidate to use a tie()ed hash DBfile
rather than a flat text file read in and out.... Be a lot faster and
use less memory probably.

If you insist on a flat text file, and its not *huge* I guess you
could read it all into a hash in memory, do your thing, and then write
it all back out (overwriting the old file).

It also looks like a good candidate for DBD::CSV, which wouldn't require
any changes to the file format and which has the insert/delete/select
functionality already written. But if that's overkill, then Tie::File
(which is included with the standard Perl distribution) would probably be
helpful.
 
B

blnukem

Eric Bohlman said:
(e-mail address removed) (dan baker) wrote in


It also looks like a good candidate for DBD::CSV, which wouldn't require
any changes to the file format and which has the insert/delete/select
functionality already written. But if that's overkill, then Tie::File
(which is included with the standard Perl distribution) would probably be
helpful.


I got it working.
 
J

Joe Smith

blnukem said:
incoming array:

foreach $Pair (@Pairs) {
($UniqeID, $Quantity) = split(/=/, $Pair);
}

Equal sign? Your original post did not mention anything about
"=" as part of the data. Now you've got us all confused.
Please try again, showing us actual code and actual input data.

Your "updated input values" belong in a hash, not an array.
Once you've got the hash set up, you can then process the
flat file a line at a time.
-Joe
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top