Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > looping through array

Reply
Thread Tools

looping through array

 
 
Julia De Silva
Guest
Posts: n/a
 
      07-11-2004
> 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
> --
> www.gh-webinteractive.com
>


Is that a FAQ or What !! perlfaq4 I think



 
Reply With Quote
 
 
 
 
svenne
Guest
Posts: n/a
 
      07-11-2004
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
--
www.gh-webinteractive.com
*
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-12-2004
Peter Payne <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

[ top post fixed - do not top post ]

> svenne <bobsmith@[no-thankyou-very-much]jippii.fi> wrote:


>> 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?


--
A. Sinan Unur
http://www.velocityreviews.com/forums/(E-Mail Removed)d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      07-12-2004
[Please do not top-post; trying to correct]
Peter Payne wrote:
> svenne <bobsmith@[no-thankyou-very-much]jippii.fi> wrote:
>> 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++ )


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


 
Reply With Quote
 
svenne
Guest
Posts: n/a
 
      07-12-2004
Peter Payne wrote:

> 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 <bobsmith@[no-thankyou-very-much]jippii.fi> wrote:
>> 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!

--
--
www.gh-webinteractive.com

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-12-2004
svenne <bobsmith@[no-thankyou-very-much]jippii.fi> wrote in news:9NrIc.3073
$(E-Mail Removed):

> 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

--
A. Sinan Unur
(E-Mail Removed)d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
looping through json array loops through the characters instead ofthe values Aaron Javascript 2 04-10-2011 05:58 PM
Looping through tw-dimensional array neptune6644@gmail.com ASP General 2 12-01-2006 02:17 AM
looping through array to find next matching element Psybar Phreak Java 1 10-06-2003 06:01 PM
Method not recognizing PAREN when looping through character array Curts Java 1 08-21-2003 10:47 PM



Advertisments