looping through array

J

Julia De Silva

Hi all,
this is trivvial question but how do you compare existense of elements intwo
arrays?, that is I want to compare two arrays and check the existense of
the first array's elements in the second array,

one line of code if possible...

any advice much appreciated, thank you.
/G

Is that a FAQ or What !! perlfaq4 I think
 
S

svenne

Hi all,
this is trivvial question but how do you compare existense of elements intwo
arrays?, that is I want to compare two arrays and check the existense of
the first array's elements in the second array,

one line of code if possible...

any advice much appreciated, thank you.
/G
 
A

A. Sinan Unur

[ top post fixed - do not top post ]
this is trivvial question but how do you compare existense of
elements intwo arrays?, that is I want to compare two arrays and
check the existense of the first array's elements in the second
array,

my %h_array2 = map { $_, 1 } @ar_array2;

for ( my $i = 0; $i < scalar( @ar_array1 ); $i++ )
{
if ( exists( $h_array2{ $ar_array1[$i] } ) )
{
print( " Element \""
. $ar_array1[$i]
. "\" exists in array2!\n" );
}
}

why did I ever become a programmer??

Good question. Why don't you take a look at perldoc -q intersection?
 
J

Jürgen Exner

[Please do not top-post; trying to correct]
Peter said:
my %h_array2 = map { $_, 1 } @ar_array2;
for ( my $i = 0; $i < scalar( @ar_array1 ); $i++ )

Why this C-style loop? There is no need to trace the index!
Just do
for (@ar_array1) {
{
if ( exists( $h_array2{ $ar_array1[$i] } ) )

Then this line becomes an easier
if ( exists( $h_array2{ $_ } ) )
{
print( " Element \""
. $ar_array1[$i]
. "\" exists in array2!\n" );

And the whole print statement become a simple
print "Element \"$_\" exists in array2!\n";
}
}

Ooh ooh my head hurts, ooh that was SOOO hard, oh oh oh why did
I ever become a programmer?? I never expected to have to THINK
for myself.. cruel world..

However, the best solution is still the answer in the FAQ: "perldoc -q
intersection"

jue
 
S

svenne

Peter said:
my %h_array2 = map { $_, 1 } @ar_array2;

for ( my $i = 0; $i < scalar( @ar_array1 ); $i++ )
{
if ( exists( $h_array2{ $ar_array1[$i] } ) )
{
print( " Element \""
. $ar_array1[$i]
. "\" exists in array2!\n" );
}
}

Ooh ooh my head hurts, ooh that was SOOO hard, oh oh oh why did
I ever become a programmer?? I never expected to have to THINK
for myself.. cruel world..

svenne said:
this is trivvial question but how do you compare existense of elements
intwo arrays?, that is I want to compare two arrays and check the
existense of the first array's elements in the second array,

one line of code if possible...

any advice much appreciated, thank you.
/G
so basically
my %h_array2 = map { $_, 1 } @ar_array2;
for ( my $i = 0; $i < scalar( @ar_array1 ); $i++ )
{
if ( exists( $h_array2{ $ar_array1[$i] } ) ){
return true;
}
return false;
}
....is what I need, thank you for replying, *S*
have a great day!
 
A

A. Sinan Unur

so basically
my %h_array2 = map { $_, 1 } @ar_array2;
for ( my $i = 0; $i < scalar( @ar_array1 ); $i++ )
{
if ( exists( $h_array2{ $ar_array1[$i] } ) ){
return true;
}
return false;
}
...is what I need,

In case you do not mean that sarcastically, please see

perldoc -q intersection
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top