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

Re^9: incrementing already existing file

by broomduster (Priest)
on Mar 02, 2011 at 23:11 UTC ( #891127=note: print w/ replies, xml ) Need Help??

in reply to Re^8: incrementing already existing file
in thread incrementing already existing file

Show us the code you tried. For that desired output, the modifications to the code I posted are not that complicated, especially with the suggestions that I already made. What I already said about collecting values from MYFILE will still work. Here are some additional hints to create the output you want:

  • For each line from NEWF, you need to check the index in column 5 and the A/B label in the last column. You get your hands on each of those with something like this:
    my @fields = ( split /\s+/ ); # need to hold on to all field +s for later my( $index, $label ) = @fields[4, -1]; # this is an array slice
  • Now that you know the index and the label for a line, you use them to find the proper value from one of the arrays you created while reading from MYFILE (e.g., @a_vals or @b_vals if you followed my example). As I mentioned, before, you can use if/else to do that but a nicer way is with given/when (as long as your Perl is 5.10.0 or newer). Here's a suggestion but note that it will not work as written here. $x and $y are not declared. You need to replace them with the correct value from @a_vals or @b_vals (HINT: use $index from above).
    use 5.010; given( $label ) { when ( 'A' ) { $fields[ -2 ] = $x; } when ( 'B' ) { $fields[ -2 ] = $y; } default { say "OOPS! bad label"; } }
  • Now you can print the result:
    say join "\t", @fields; # separates fields with TAB

That's all there is to it. Give it another go and post back with your code if you need more help.

Comment on Re^9: incrementing already existing file
Select or Download Code
Re^10: incrementing already existing file
by wanttoprogram (Novice) on Mar 03, 2011 at 06:16 UTC
    #!/usr/bin/env perl use strict; use warnings; my $file1 = "2hgs_d00_internal_nrg_e.dat"; my $file2 = "2HGS_bio_conv-min_p.pdb"; open( MYFILE, '<', $file1 ) or die "cannot open $file1: $!"; open( NEWF, '<', $file2 ) or die "cannot open $file2: $!"; my (@a_vals, @b_vals); while ( <MYFILE> ) { chomp; my( $label, $index, $value ) = ( split /\s+/ )[3, 5, -1]; $a_vals[ $index ] = $value; $b_vals[ $index ] = $value; } close MYFILE; while ( <NEWF> ) { chomp; my @fields = ( split /\s+/ ); my $index = $fields[4]; my $label = $fields[-1]; if ($label eq "A"){ $fields[-2] = $a_vals[ $index ] } if ($label eq "B"){ $fields[-2] = $b_vals[ $index ] } my $output = join "\t", @fields; print "$output\n"; } close NEWF;
    I am still getting values from B from MYFILE
      In this section of your code:
      while ( <MYFILE> ) { chomp; my( $label, $index, $value ) = ( split /\s+/ )[3, 5, -1]; $a_vals[ $index ] = $value; $b_vals[ $index ] = $value; }
      You need to check the value of $index to get the values for type 'A' into @a_vals, similarly for the 'B' values. The way you have it, you only get 'B' values because they are the last ones in MYFILE, and you are putting the same things into both @a_vals and @b_vals. Since the 'B' values come in last, they overwrite the corresponding 'A' values in @a_vals. You can use the same kind of if/elsif/else logic as in your other loop to fix this(but see comments below on ways to improve that).

      Now for this part of the loop over NEWF:

      if ($label eq "A"){ $fields[-2] = $a_vals[ $index ] } if ($label eq "B"){ $fields[-2] = $b_vals[ $index ]

      You should use if / elsif / else here:

      if ($label eq "A") { $fields[-2] = $a_vals[ $index ] } elsif ($label eq "B") { $fields[-2] = $b_vals[ $index ] } else { print "OOPS! bad label\n"; # complain if something is fishy }

      You're getting there.

        Perfect. It worked. Thank you for your clean answers and time.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://891127]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-07-22 18:49 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (126 votes), past polls