Update multiple lines in a flat file from array

Discussion in 'Perl Misc' started by blnukem, Sep 4, 2004.

  1. blnukem

    blnukem Guest

    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
    blnukem, Sep 4, 2004
    #1
    1. Advertising

  2. blnukem

    Paul Lalli Guest

    blnukem wrote:
    > 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
    Paul Lalli, Sep 4, 2004
    #2
    1. Advertising

  3. blnukem

    blnukem Guest

    incoming array:

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



    "Paul Lalli" <> wrote in message
    news:chcanf$mjt$...
    > blnukem wrote:
    > > 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
    blnukem, Sep 5, 2004
    #3
  4. blnukem

    Paul Lalli Guest

    [top posting rearranged as it should be]

    blnukem wrote:
    > "Paul Lalli" <> wrote
    >>blnukem wrote:
    >>
    >>>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.
    >>

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


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


    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
    Paul Lalli, Sep 5, 2004
    #4
  5. blnukem

    dan baker Guest

    "blnukem" <> wrote in message news:<Y2i_c.5572$>...
    > 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
    dan baker, Sep 5, 2004
    #5
  6. blnukem

    Eric Bohlman Guest

    (dan baker) wrote in
    news::

    > "blnukem" <> wrote in message
    > news:<Y2i_c.5572$>...
    >> 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).


    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.
    Eric Bohlman, Sep 5, 2004
    #6
  7. blnukem

    blnukem Guest

    "Eric Bohlman" <> wrote in message
    news:Xns955BAE75BB06Febohlmanomsdevcom@130.133.1.4...
    > (dan baker) wrote in
    > news::
    >
    > > "blnukem" <> wrote in message
    > > news:<Y2i_c.5572$>...
    > >> 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).

    >
    > 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.
    blnukem, Sep 6, 2004
    #7
  8. blnukem

    Joe Smith Guest

    blnukem wrote:

    > 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
    Joe Smith, Sep 6, 2004
    #8
    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. Mohammad S Khan
    Replies:
    3
    Views:
    8,662
    Chris Uppal
    Aug 31, 2004
  2. Rhino
    Replies:
    4
    Views:
    725
    Rhino
    Mar 24, 2005
  3. Peter Steiner
    Replies:
    2
    Views:
    446
    Peter Steiner
    Nov 3, 2005
  4. Darrell L. Schmitt
    Replies:
    1
    Views:
    101
    Bob Walton
    Aug 29, 2004
  5. Cah Sableng
    Replies:
    0
    Views:
    231
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page