here is my updated code
use strict; use warnings;
Yeah!! :)
while(<IBDIN>){ my @array_a = split ' ', <IBDIN>; my @array_b = split ' ', <IBDIN>; compare_array(); }
By declaring the arrays within a while loop, they are not accessible outside the loop. That's why Perl complains. (Suggested reading, since you seem not to be used to strictures: http://perl.plover.com/FAQs/Namespaces.html)
This is what I first suggested:
my @array_a = split ' ', <IBDIN>; my @array_b = split ' ', <IBDIN>; compare_array();
i.e. no loop at all. If you prefer a while loop, two named arrays is not very suitable. In that case, you may use an array of arrays instead:
my @arrays; while(<IBDIN>){ @{ $arrays[$.] } = split ' '; } compare_array();
But if you are not familiar with Perl data structures, why not stick to the option without a while loop.
sub compare_array { my (%seen, @bonly); @[EMAIL PROTECTED] = (); # build lookup table foreach my $item (@array_b){ push(@bonly, $item) unless exists $seen{$item}; } print "@bonly \n"; }
Since you now are using a subroutine, I'd like to make a comment with respect to style. It's good programming practice to pass variables to a subroutine rather than having it process file scoped variables. With that in mind, I'd suggest the following:
open IBDIN, "<$file" or die "cannot open $file $!"; my @array_a = split ' ', <IBDIN>; my @array_b = split ' ', <IBDIN>; close IBDIN;
compare_array( [EMAIL PROTECTED], [EMAIL PROTECTED] );
sub compare_array { my ($ref_a, $ref_b) = @_; my (%seen, @bonly); @seen{ @$ref_a } = (); # build lookup table foreach my $item ( @$ref_b ) { push (@bonly, $item) unless exists $seen{$item}; } print "@bonly \n"; }
-- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>