perl - Using goto LABEL for comparing two files -
i unable desired output.
please correct errors.
file1
a b c d e f
file2
a d c
desired output (if found print '1' @ relative position in larger file , if not print '0')
1
0
1
1
0
0
code
#!/usr/bin/perl -w open(fh,$file); @q=<fh>; open(fh1,$file2); @d=<fh1>; open(out,">out.txt"); foreach $i(@q) { foreach $j(@d) { if ($i eq $j) { $id=1 ; goto label; } elsif ($i ne $j) { $id=1; goto label; } } } print out "1\t"; label: print out "0\t"; } close fh; close fh1; close out;
note: actual files much larger , contain uneven number of elements.
you looking for
for $q (@q) { $found = 0; $d (@d) { if ($q eq $d) { $found = 1; goto label; } } label: print "$found\n"; }
the above better written follows:
for $q (@q) { $found = 0; $d (@d) { if ($q eq $d) { $found = 1; last; } } print "$found\n"; }
but solutions perform poorly. can avoid iterating on @d
repeatedly using hash.
my %d = map { $_ => 1 } @d; $q (@q) { print $d{$q} ? "1" : "0", "\n"; }
Comments
Post a Comment