简体   繁体   中英

Perl: Difference between two array index values

I have two arrays input:

@a = (1, 2, 3, 4, 5);
@b = (2, 2, 3, 6, 8);

Output:

print 1,4,5 difference in first array.
print 2,6,8 difference in second array.

I tried this, but I don't no which module to use.

use Array::Utils qw(:all);

@a = (1, 2, 3, 4, 5);
@a = (2, 2, 3, 6, 8);

# get items from array @a that are not in array @b
my @minus = array_minus( @a, @b );
print @minus;exit;

I want to print the difference between index values.

my @diff_indexes = grep { $a[$_] != $b[$_] } 0..$#a;

my @a_diff_values = @a[ @diff_indexes ];
my @b_diff_values = @b[ @diff_indexes ];

Reference: grep , array slice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM