in reply to Match but ignore white space
I know that in s/// or m/// the /x will ignore white space ...The /x modifier will allow you to add white space in your regular expression to make it more readable - it doesn't ignore white space in the text that you are trying to match. Example:
my $x = "foobar"; print "matches 1\n" if ($x =~ m/foo bar/x); # succeeds print "matches 2\n" if ($x =~ m/foo bar/); # fails
humm just had a thought ... how about running through the array and just removing all white space and dump into @clean, then do the same to $line just prior to the grepThis is a good idea. You are canonicalizing the input, mapping each line to a single representative so that you can just use string equality to test if the lines are "the same."
Another common perl-ish approach is to use a hash. This will avoid iterating through the the @clean array for each line in the second file:
This run time is essentially linear in the number of lines of each file.my %hash; while (<file1>) { my $clean = ... convert $_ into its canonical form ... $hash{$clean} = 1; } while (<file2>) { my $clean = ... convert $_ into its canonical form ... if ($hash{$clean}) { # line from file2 exists in file1 } else { # line from file2 is not in file1 } }
|
|---|