DS has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys ,, I have two files , one contain data like this
data.txt hello.pl~45~warning~454~null pointer hello.pl~43~warning~454~memory hello.pl~4532~warning~454~badpointer hello.pl~22~warning~454~null pointer hello.pl~88~warning~454~null pointer
where the first number in each line indcating the line number. in my second file I have only line numbers that been changed , somthing like this
lineChanged.txt 22-33 90 42-44 100
where 22-33 indicating that line 22 to 33 have changed. so what I need to do is reading from the lineChanged.txt file each line and see if they appear in data.txt or they are in the range , if it does then I want to print that line from the data.txt and send it to a an output file , so in my example , my output will be
hello.pl~43~warning~454~memory hello.pl~22~warning~454~null pointer
since they appear in both files I am doing somthing like this right now but it is not doing the work
while (<CHANGES>) { chomp; # split the line from the spaces # $file is file name like hello.pl $info = $_; is it a range or a single line? if ($info =~ /(\d+)-(\d+)/) { $changes{$file} .= " ".join(" ",($1 .. $2))." "; } else { $changes{$file} .= " $info "; } if (defined $changes{$file}) { # was it a range? if (defined $changes{$file}->[1]) { # skip if this $line is not in this range next unless ($line gt $changes{$fill}->[0] and $line lt $change +s{$file}->[1]); } else { # skip if this $line is not equal to this single line number next unless $line eq $changes{$file}->[0]; } # if we are here, then this is a match. so print it. print "############ Here ################\n"; # print "$_\n"; #print "############ Here ################\n"; }
can someone tell me an easir way or tell me what is it wrong I am doing .. thanks for help

Replies are listed 'Best First'.
Re: regex with comparing
by seattlejohn (Deacon) on Jul 19, 2002 at 05:24 UTC
    I would think about reading the change file and creating a hash whose keys are all the lines that have changed, something like this:
    if ($info =~ /(\d+)-(\d+)/) { $changes{$file}{$_} = 1 foreach ($1..$2) } else { $changes{$file}{$info} = 1; }

    Then, in a separate loop, I'd iterate through the lines in the data file and test each one, something like this:

    $line =~ m/~(\d+)~/; print $line if $changes{$file}{$1};
Re: regex with comparing
by tadman (Prior) on Jul 19, 2002 at 05:18 UTC
    I have no idea what you're getting at, because this code looks like Perl, and maybe smells like Perl, but it sure as heck won't run. It appears to be missing major parts. For example, where is $file assigned? How do you read two files with only one file handle?

    use strict and -w. Begin with something like this:
    #!/usr/bin/perl -w use strict; # (More program ...)
    That being said, you're probably going to get a whole ton of warnings. For instance, you're trying to use a string as an array reference. There's a comment with no comment prefix.

    Don't get ahead of yourself. Read in the changes file first, and then worry about the rest. Now here's an idea on how you can read it in.
    open(CHANGED, "changed.txt") || die "Can't open change log\n"; my @changes = map { chomp; [ split('-', $_) ] } <CHANGED>; close(CHANGED);
    This makes an array of arrays (AoA). You can read this like you expect with the rest of your code. If you want to put this in a hash, like you've suggested, thus making a hash of array of arrays (HoAoA), try using this in the loop:
    $changes{$file} = \@changes;
    Now that you've got the data, the rest should be a lot easier.