Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found