Searching the information on the Array by using RepExp

P

phal

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
 
J

John W. Krahn

phal said:
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top