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

I am using array util package for array comparison.I have reading two file single and single 2 in difference2.pl and comparing but it is showing the only the element present in array c.Plz tell me where I am doing mistake.
use Array::Utils qw(:all); $file = 'single.txt'; open(FILE,$file); @array= <FILE>; #print @array; $file1='single1.txt'; open(FILE1,$file1); @array1=<FILE1>; @d=@array1; @c=@array; print @d; print @c; # symmetric difference @diff = array_diff(@a, @b); # intersection @isect = intersect(@c, @d); #print @isect; # unique union @unique = unique(@a, @b); #print @unique; # check if arrays contain same members if ( !array_diff(@a, @b) ) { # do something } # get items from array @a that are not in array @b @minus = array_minus( @c,@d ); print "\n difference"; print @minus;
I want the output of @minus.But is is showing the array c. Single file contains element :1,2,3,4 Single1 file contains element :1,5,6,7 Ideally the output should be:2,3,4. plz help me.

Replies are listed 'Best First'.
Re: querybregarding array utils module in perl
by tangent (Parson) on Jun 26, 2014 at 15:31 UTC
    If your files contain a single line with the array elements separated by commas then you will need to split the line, otherwise @array will contain just a single element ("1,2,3,4") and @array1 will also contain just a single element ("1,5,6,7"). Try reading the files in this way:
    use strict; use warnings; use Array::Utils qw(:all); my $file = 'single.txt'; my $file1 = 'single1.txt'; my $fh; my @array; open($fh,"<",$file) or die("Could not open $file: $!"); while (my $line = <$fh>) { chomp $line; next unless $line; push(@array, split(',',$line) ); } close($fh); my @array1; open($fh,"<",$file1) or die("Could not open $file1: $!"); while (my $line = <$fh>) { chomp $line; next unless $line; push(@array1, split(',',$line) ); } close($fh);
Re: querybregarding array utils module in perl
by kennethk (Abbot) on Jun 26, 2014 at 15:33 UTC
    Are you sure your arrays contain what you think they do? See Basic debugging checklist. Since you haven't provided your literal input files wrapped in <code> tags (see How do I post a question effectively?), it's hard to be helpful. Two possibilities I see:
    1. Your second open is failing, and thus you don't know that @array1 is empty. It's probably better if your opens look more like: open(FILE,'<', $file) or die "Open fail on $file: $!"

    2. Your input is literally 1,2,3,4 and so your arrays only have 1 element. In this case, you need to reformat your input files to be newline separated or feed the input through split.

    The following code works for me.

    #!/usr/bin/perl -w use strict; use Array::Utils qw(:all); my @first = (1,2,3,4); my @second = (1,5,6,7); my @minus = array_minus( @first,@second ); for my $term (@minus) { print "'$term'\n"; }

    It outputs

    '2' '3' '4'

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.