Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Best way to compare range of characters in two text files

by vinoth.ree (Monsignor)
on Jul 12, 2018 at 04:42 UTC ( [id://1218357]=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi

Basically I need to compare range(1-50) of characters in two files. I am planning to first open and read the first 50 characters in each file and store in a temporary file and compare both the files. This way I can fix my issue. Could anyone suggest better solution for this ?

update:

1.Instead of writing into temp file I can store the content into two different array and use List::Compare module two find the difference.


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

Replies are listed 'Best First'.
Re: Best way to compare range of characters in two text files
by davido (Cardinal) on Jul 12, 2018 at 06:22 UTC

    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

      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

Re: Best way to compare range of characters in two text files
by Laurent_R (Canon) on Jul 12, 2018 at 07:36 UTC
    If you know how to read the first 50 characters from each file, why don't you simply read them into variables and then compare the variables? That should be much simpler.

    I can't say much more right now because it is not clear to me what you mean by comparing the first 50 characters: do you just need to know if they are equal? Do you need to find the differences? Also, are the files text files? Please provide more details.

Re: Best way to compare range of characters in two text files
by tybalt89 (Monsignor) on Jul 12, 2018 at 13:36 UTC
    sub compare50 { my ($file1, $file2) = @_; local $/ = \50; return do {local @ARGV = $file1; <> } eq do {local @ARGV = $file2; < +> }; }

    untested...

      >  $/ = \50;

      Thanks, learned something new about the $INPUT_RECORD_SEPARATOR today. :)

      perlvar $/

      Setting $/ to a reference to an integer ... will attempt to read records instead of lines ...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1218357]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found