looping through array

Discussion in 'Perl Misc' started by Julia De Silva, Jul 11, 2004.

  1. > 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
     
    Julia De Silva, Jul 11, 2004
    #1
    1. Advertising

  2. Julia De Silva

    svenne Guest

    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
     
     
    svenne, Jul 11, 2004
    #2
    1. Advertising

  3. Peter Payne <> wrote in
    news::

    [ 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
    d
    (remove '.invalid' and reverse each component for email address)
     
    A. Sinan Unur, Jul 12, 2004
    #3
  4. [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
     
    Jürgen Exner, Jul 12, 2004
    #4
  5. Julia De Silva

    svenne Guest

    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
     
    svenne, Jul 12, 2004
    #5
  6. svenne <bobsmith@[no-thankyou-very-much]jippii.fi> wrote in news:9NrIc.3073
    $:

    > 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
    d
    (remove '.invalid' and reverse each component for email address)
     
    A. Sinan Unur, Jul 12, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curts
    Replies:
    1
    Views:
    334
    Lee Fesperman
    Aug 21, 2003
  2. Psybar Phreak
    Replies:
    1
    Views:
    573
    Eric Sosman
    Oct 6, 2003
  3. Replies:
    2
    Views:
    104
  4. Aaron
    Replies:
    2
    Views:
    542
    dhtml
    Apr 10, 2011
  5. Replies:
    5
    Views:
    288
Loading...

Share This Page