Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > comparing two arrays in perl

Reply
Thread Tools

comparing two arrays in perl

 
 
bcdixit
Guest
Posts: n/a
 
      08-27-2007
Hi,
I have two arrays, for example,

@array = ("john","larry","kevin","peter","mike")
@exclude = ("john","kevin")

i want to delete from @array , values that exist in @exclude.
i.e. i want my @array to look like

@array("","larry","","peter","mike")

i have written this code but it doesnot seem to work.

foreach$i(0..$#exclude)
{
foreach$x(0..$#array)
{
if($array[$x] eq $exclude[$i])
{
delete($array[$x]);
next;
}
}
}

am I missing something? is there someother better way to do it in
perl?

thanks
-bd

 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      08-27-2007
bcdixit <(E-Mail Removed)> writes:

> @array = ("john","larry","kevin","peter","mike")
> @exclude = ("john","kevin")
>
> i want to delete from @array , values that exist in @exclude.
> i.e. i want my @array to look like


What you need is an easy way to check if an value is in @exclude. This
can be done by converting it to a hash where easch element from
@exclude is an key with an true value:

This can be done this way

%exclude = map { $_ => 1 } @exclude;

or this way:

$exclude{$_} = 1 for @exclude;

(and probbaly many other ways).

Then you just need to iterate over @array one time.

//Makholm
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-27-2007
On Aug 27, 2:17 pm, bcdixit <(E-Mail Removed)> wrote:
> I have two arrays, for example,
>
> @array = ("john","larry","kevin","peter","mike")
> @exclude = ("john","kevin")
>
> i want to delete from @array , values that exist in @exclude.


Your Question (or at least a variant of it) is Frequently Asked...
$ perldoc -q "difference of two arrays"
Found in /opt/perl/lib/5.6.1/pod/perlfaq4.pod
How do I compute the difference of two arrays? How do I
compute the intersection of two arrays?

> i.e. i want my @array to look like
>
> @array("","larry","","peter","mike")
>
> i have written this code but it doesnot seem to work.


Does not work is the worst possible error description. *HOW* does it
not work? What results does it produce that you did not expect?

>
> foreach$i(0..$#exclude)
> {
> foreach$x(0..$#array)
> {
> if($array[$x] eq $exclude[$i])
> {
> delete($array[$x]);


delete(), when used on array elements, will undefine internal
elements, and will remove the last element. This is generally not a
good thing, and also doesn't match your desired output. I think you
instead want:
$array[$x] = "";

> next;


"next" being the last statement in a block is a no-op. It simply says
to go up to the beginning of the current loop and start the next
iteration. That's what would happen anyway. I think you meant "last"
here.

> }
> }
> }
>
> am I missing something?


You tell me. The code (after being modified as I suggest) does
exactly what you say you want. If you disagree, post a short-but-
complete script with desired output and actual output.

> is there someother better way to do it in perl?


Yes, use hashes, as Peter and the FAQ both suggested.

Paul Lalli

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      08-27-2007
bcdixit wrote:
> I have two arrays, for example,
>
> @array = ("john","larry","kevin","peter","mike")
> @exclude = ("john","kevin")
>
> i want to delete from @array , values that exist in @exclude.
> i.e. i want my @array to look like
>
> @array("","larry","","peter","mike")
>
> i have written this code but it doesnot seem to work.
>
> foreach$i(0..$#exclude) {
> foreach$x(0..$#array) {
> if($array[$x] eq $exclude[$i]){
> delete($array[$x]);

[...]
> am I missing something?


Yes. It is very bad to change the array (add or remove elements) while
looping through it using foreach().

> is there someother better way to do it in perl?


Like explained in the FAQ: "How do I compute the difference of two arrays?
How do I compute the intersection of two arrays?"

jue


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-27-2007
On Aug 27, 2:55 pm, "Jürgen Exner" <(E-Mail Removed)> wrote:
> bcdixit wrote:


> > foreach$i(0..$#exclude) {
> > foreach$x(0..$#array) {
> > if($array[$x] eq $exclude[$i]){
> > delete($array[$x]);

> [...]
> > am I missing something?

>
> Yes. It is very bad to change the array (add or remove elements)\
> while looping through it using foreach().


The only element that could be deleted from that array is the last
one. I don't see how that could affect anything, as it would also
therefore be the last iteration of the loop.

Paul Lalli

 
Reply With Quote
 
jkstill
Guest
Posts: n/a
 
      08-28-2007
On Aug 27, 11:17 am, bcdixit <(E-Mail Removed)> wrote:
> Hi,
> I have two arrays, for example,
>
> @array = ("john","larry","kevin","peter","mike")
> @exclude = ("john","kevin")
>
> i want to delete from @array , values that exist in @exclude.
> i.e. i want my @array to look like
>
> @array("","larry","","peter","mike")
>
> i have written this code but it doesnot seem to work.
>
> foreach$i(0..$#exclude)
> {
> foreach$x(0..$#array)
> {
> if($array[$x] eq $exclude[$i])
> {
> delete($array[$x]);
> next;
> }
> }
> }
>
> am I missing something? is there someother better way to do it in
> perl?
>
> thanks
> -bd


This looks promising:

http://www.perlmonks.org/index.pl?node=429761


 
Reply With Quote
 
jkstill
Guest
Posts: n/a
 
      08-28-2007
On Aug 28, 11:34 am, jkstill <(E-Mail Removed)> wrote:
> On Aug 27, 11:17 am, bcdixit <(E-Mail Removed)> wrote:
>
>
>
> > Hi,
> > I have two arrays, for example,

>
> > @array = ("john","larry","kevin","peter","mike")
> > @exclude = ("john","kevin")

>
> > i want to delete from @array , values that exist in @exclude.
> > i.e. i want my @array to look like

>
> > @array("","larry","","peter","mike")

>
> > i have written this code but it doesnot seem to work.

>
> > foreach$i(0..$#exclude)
> > {
> > foreach$x(0..$#array)
> > {
> > if($array[$x] eq $exclude[$i])
> > {
> > delete($array[$x]);
> > next;
> > }
> > }
> > }

>
> > am I missing something? is there someother better way to do it in
> > perl?

>
> > thanks
> > -bd

>
> This looks promising:
>
> http://www.perlmonks.org/index.pl?node=429761


My bad, replied to the wrong thread.

 
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
FAQ 4.43 How do I compute the difference of two arrays? How do I compute the intersection of two arrays? PerlFAQ Server Perl Misc 0 02-02-2011 05:00 AM
two arrays problem (although different from the other two arrays) Kev Jackson Ruby 2 03-29-2006 03:58 PM
Comparing values between two arrays darrel ASP .Net 2 12-23-2005 05:11 PM
Help comparing two arrays Miss Michelle. Heigardt Java 2 07-30-2005 06:09 PM
comparing two arrays of integers. Nag Java 3 06-23-2005 06:16 PM



Advertisments