Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
With your program Newfile is replaced by B set of values. How do I make it to take A set first and go to B set.
So what you have is two collections of numbers in the file you open as MYFILE. One set is labeled 'A' in column 3 and the other is labeled 'B' in column 3. The values in column 5 are an index, and the values in the last column are the ones you need to collect and add to NEWF.

What I said before is still mostly what you need to do. Here's a revised outline of the code you need to write:

  • Declare two arrays, one to hold the values from lines where column 3 is 'A' and another for lines where column 3 is 'B'. Something like:
    my( @a_vals, @b_vals );
  • Read each line from MYFILE, use split to take it apart:
    my( $type, $index, $value ) = ( split /\s+/ )[2, 4, -1];
  • Now you just put $value into the appropriate array by checking what $type is. An easy way to do that is with if/else, but I'm going to let you figure out the details. If you have Perl 5.10 or later, a nice way to handle this is with given/when.
  • Now change the while ( <NEWF> ) loop to insert the collected values where you want them. You get to figure out that part, as well.

You didn't show a sample of what the output should look like, so this is as far as I can go with what you've posted. If the output should have both 'A' and 'B' values at the end of a line, you do one thing. If you need two lines (one for each of the 'A' and 'B' values), you do something different. But we can't know unless you tell us. Which leads me to the following advice about how to get along well on PerlMonks:

  • Show your code. You did that. ++
  • Show sample data. You did that, but we had to ask more than once. And this last time, you posted way more data than necessary to show a typical sample. About five lines of each of the 'A' and 'B' types would have been enough.
  • Show the output you're getting and the output you want. You did that after we asked the first time, but when you came back with more questions and I asked for sample input and sample output, you posted input but no sample output.

I think you have enough additional information now to "fix" my code to do what you want. Give it a shot and come back with questions if you run into trouble.

In reply to Re^7: incrementing already existing file by broomduster
in thread incrementing already existing file by wanttoprogram

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    NodeReaper reads the Necronomicon

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2018-06-24 11:29 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.