in reply to Best way to compare range of characters in two text files

You know the drill by now. :)

Post sample input, sample desired output, code you've tried, and a detailed explanation of the criteria you wish to use in the comparison.

Your problem description leaves me with no understanding of why you would need two temporary files to use in comparing the first fifty characters in two files.


Dave

  • Comment on Re: Best way to compare range of characters in two text files

Replies are listed 'Best First'.
Re^2: Best way to compare range of characters in two text files
by vinoth.ree (Monsignor) on Jul 13, 2018 at 04:26 UTC
    Hi davido,

    Actually I stuck with some other work, so could not post whatever I tries so far. Pls find the below code. Actually, the requirement is to find the changed lines in the second file and check all those changes have proper tag. Tag is nothing but the ticket number. The use will make their changes within the first chracters of each line, after that other characters will have comment about the line(contains ticket number also).

    use strict; use warnings; use Data::Dumper; use FileIO; # own module to do file operations my $old_file = shift; my $new_file = shift; my $tkt_no = shift; my @old_version; my @new_version; my $error; #reads the file into array if (not FileIO::read_file_strip($old_file,\@old_version,\$error)){ print $error; return; } if (not FileIO::read_file_strip($new_file,\@new_version,\$error)){ print $error; return; } my %second_file; foreach my $oline ( @old_version ){ my $oshort_line = substr( $oline, 0, 49 ); $second_file{unpack 'A*', $oshort_line} = $oline; } foreach my $nline (@new_version) { my $nshort_line = substr( $nline, 0, 49 ); if( not exists $second_file{unpack 'A*', $nshort_line}){ my $tmp_str = substr($nline,50,-1); if ( defined $tmp_str and $tmp_str ne ""){ if ( $tmp_str =~ /$tkt_no/){ print("Tag Valid for $nline\n"); } }else{ print("Tag not found for $nline\n"); } } }

    All is well. I learn by answering your questions...

      G'day vinoth.ree,

      After reading this in your OP:

      "... read the first 50 characters ..."

      these two lines in your code grabbed my attention:

      ... my $oshort_line = substr( $oline, 0, 49 ); ... my $nshort_line = substr( $nline, 0, 49 ); ...

      I don't know if you thought that's an inclusive range, or something else. See the substr documentation:

      "substr EXPR,OFFSET,LENGTH"

      To get the first 50 characters from a string, you need OFFSET==0 and LENGTH==50. Here's an example of what you have and one of what you probably want:

      $ perl -E 'my $x = "X" x 49 . "Y" x 49; say substr $x, 0, 49' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX $ perl -E 'my $x = "X" x 49 . "Y" x 49; say substr $x, 0, 50' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXY

      — Ken

        You are right, Actually this script is for other program. I could have posted the other script.


        All is well. I learn by answering your questions...