Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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 ( #1218420=note: print w/replies, xml ) Need Help??


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

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...

Replies are listed 'Best First'.
Re^3: Best way to compare range of characters in two text files
by kcott (Bishop) on Jul 13, 2018 at 07:17 UTC

    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...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2020-09-19 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (114 votes). Check out past polls.

    Notices?