Searching the information on the Array by using RepExp

Discussion in 'Perl Misc' started by phal, Oct 11, 2005.

  1. phal

    phal Guest

    Dear Guru

    I have two array

    my @array1=(1,2,3,4,5,6);
    my @array2=(1,4);
    my %temp;

    for(my $x=0; $x<@array1; $x++)
    {
    if(@array2!=~/$array1[$x]/)
    {
    $temp{$array1[$x]}=$array1[$x];

    }
    next if(@array2 =~ /$array1[$x]);

    }

    If any information already record in the @array2, then %temp will not
    record that data, so the result so be

    %temp=(2=>2,3=>3,5=>5,6=>6);

    but it does work, coz it just record all the information
    phal, Oct 11, 2005
    #1
    1. Advertising

  2. phal wrote:
    >
    > I have two array
    >
    > my @array1=(1,2,3,4,5,6);
    > my @array2=(1,4);
    > my %temp;
    >
    > for(my $x=0; $x<@array1; $x++)
    > {
    > if(@array2!=~/$array1[$x]/)
    > {
    > $temp{$array1[$x]}=$array1[$x];
    >
    > }
    > next if(@array2 =~ /$array1[$x]);
    >
    > }
    >
    > If any information already record in the @array2, then %temp will not
    > record that data, so the result so be
    >
    > %temp=(2=>2,3=>3,5=>5,6=>6);
    >
    > but it does work, coz it just record all the information


    It does? It sure does NOT work on my computer. For example, the line:

    if(@array2!=~/$array1[$x]/)

    is interpreted by perl as:

    if ( '2' != ~($_ =~ /$array1[$x]/) )

    and that is probably not what you want. If you want the result to be:

    %temp = ( 2 => 2, 3 => 3, 5 => 5, 6 => 6 );

    then you can do it like this:

    my @array1 = ( 1, 2, 3, 4, 5, 6 );
    my @array2 = ( 1, 4 );
    my %temp;

    @temp{ @array1 } = @array1;
    delete @temp{ @array2 };



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Oct 11, 2005
    #2
    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. Derek Simmons
    Replies:
    1
    Views:
    538
    Derek Simmons
    Mar 31, 2005
  2. z. f.
    Replies:
    0
    Views:
    670
    z. f.
    Feb 3, 2005
  3. subrato
    Replies:
    8
    Views:
    909
    Juan T. Llibre
    Mar 14, 2006
  4. diffused
    Replies:
    9
    Views:
    714
    Oscar kind
    Aug 1, 2004
  5. stumblng.tumblr
    Replies:
    1
    Views:
    192
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page