Answer the question
In order to leave comments, you need to log in
Perl, need advice - what does the above code do?
There is a code that compares two arrays and puts elements from the @spisok2 array that are not found in the @spisok1 array into the @diff array:
#! /usr/bin/perl
@spisok1=(1,2,3,4,5,6);
@spisok2=(1,2,3,4,5,7,8);
@diff=();
%seen = ();
@seen{@spisok1} = ();
foreach $item(@spisok2)
{
push(@diff, $item) unless exists $seen{$item};
}
printf ("@diff\n");
@seen{@spisok1} = ();
Answer the question
In order to leave comments, you need to log in
This line fills in a hash called %seen.
The elements of the @spisok1 array are substituted as the keys of this hash, and the values are empty.
And if we replace this line with a loop, we get:
for my $element (@spisok1) {
$seen{$element} = undef;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question