Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: compare text files

by Rudolf (Pilgrim)
on Sep 12, 2012 at 01:37 UTC ( #993111=note: print w/replies, xml ) Need Help??

in reply to compare text files

Hi linseyr, this is a script I came up with on the fly I hope it is self explanitory. The only messy part is using a huge literal because I was'nt smart enough to figure out something else.. or I was too lazy? But anyway I use file handles instead of references to them, gather the data in arrays first and then do my comparing using a variable $lesser_value to keep track of if I have the least possible value for the chrx. Best wishes, and feel free to ask any questions! hope this helps. -Rudolf

use v5.14; open(TEST,'<',"test.txt") or die $!; my @test_data = <TEST>; close(TEST); open(REFERENCE,'<',"reference.txt") or die $!; my @reference_data = <REFERENCE>; close(REFERENCE); my @results; for my $line(@test_data){ my $lesser_val = 9999999999999999; my($chr,$val) = split(/ /,$line); for my $ref_line(@reference_data){ my($ref_chr,$ref_val) = split(/ /,$ref_line); if($ref_chr eq $chr){ $lesser_val = ($ref_val-$val) if ($ref_val-$val < $lesser_val +); } } push(@results,"$chr $lesser_val"); } say foreach @results;

Replies are listed 'Best First'.
Re^2: compare text files
by linseyr (Acolyte) on Sep 12, 2012 at 03:13 UTC
    Hi Rudolf, Thank you so much for your answer! I have two questions though. 1. What does the push and say mean? I though that say is something like print, but I don't get any output now. Is it possible to print it to a file? 2. When I print $lesser_val it prints it multiple times. Should it be outside a loop or something? Thanks!

      Hey, no worries. The code does work fine on my computer; however push(@array,$item); "pushes" a flat variable onto the end of the list. So its just adding it to the list. At the end of the program outside of all loops yes I used:

      say foreach @results;

      say "hello world"; is like System.out.println() in java, it prints the output with a newline. So when I type foreach @results it just prints out each result with a newline on the end.

      Now, in order to use some keywords in perl you must tell it you are using a specific version first, I use v5.14; at the top and you can use say after... 5.10 I think. But you can also write the printout like so if you wanted to print to a results file!:

      use v5.14; open(RESULTS,'>','results.txt') or die $!; foreach(@results){ print RESULTS "$_\n"; } close(RESULTS);

      The printing should only come after all the computing.. it should not be in any loops. All results should save to the array first and then you can use it at the end. Dont hessitate with any more questions :) good luck.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993111]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-07-22 12:58 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (454 votes). Check out past polls.