Velocity Reviews > Perl > Searching the information on the Array by using RepExp

# Searching the information on the Array by using RepExp

phal
Guest
Posts: n/a

 10-11-2005
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

John W. Krahn
Guest
Posts: n/a

 10-11-2005
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