> -----Original Message-----
> From: Timothy Johnson [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, July 23, 2002 11:41 AM
> To: 'Bob Showalter '; ''Merritt Krakowitzer' '; 'Beginners '
> Subject: RE: Comparing Arrays
>
>
>
> In the "quick and dirty" category, you can do something like this:
>
> (I'm trying to remember off the top of my head)
>
> ##########################################
>
> my $result = ArrayCmp(\@array1,\@array2);
> #pass two array references
>
> sub ArrayCmp{
> my($ref1,$ref2) = @_;
> my %tmp = ();
> if(@{$ref1} != @{$ref2}){
> return 0;
> #if arrays are not same length,
> #then skip the rest
> }
> foreach(@{$ref1}){
> $tmp{$_} = 1;
> #create a temporary hash with the elements
> #as keys
> }
> foreach(@{$ref2}){
> return 0 unless $tmp{$_};
> #fail if there is not a key with each
> #element of the second array
> }
> return 1;
> #if we make it this far, they're equal
> }
But what if the @$ref1 contains an element not in @$ref2?
I just found out I can shorten my one-liner to:
@foo==@bar && "@{{map {$_, $_} @foo}}{@bar}" eq "@bar"
But mine has a similar defect:
qw(foo foo bar) is considered equal to qw(foo bar bar)
Rats! That's why to stick with proven modules!
>
> #########################################
>
>
>
> -----Original Message-----
> From: Bob Showalter
> To: 'Merritt Krakowitzer'; Beginners
> Sent: 7/23/02 5:49 AM
> Subject: RE: Comparing Arrays
>
> > -----Original Message-----
> > From: Merritt Krakowitzer [mailto:[EMAIL PROTECTED]]
> > Sent: Tuesday, July 23, 2002 3:48 AM
> > To: Beginners
> > Subject: Comparing Arrays
> >
> >
> > Hi
> >
> > I would like to know how to compare 2 arrays.
> >
> > I have 2 arrays and I would like to compare the contents of
> the data.
> > It doesn't matter in which order the data is stored so long
> > as its the same.
> > So comparing the bellow should read true, but if they didn't
> > match it would
> > be false.
> >
> > my @foo = qw(
> > foo bar cat dog
> > );
> > my @bar = qw(
> > dog cat foo bar
> > );
> >
> > Hope that made some sense.
> > I managed to find a module for comparing arrays but I would
> > prefer not to
> > do it that way.
>
> Well, they are equal without regard to order if the following is true:
>
> @foo==@bar && join($", @{{map {$_, $_} @foo}}{@bar}) eq "@bar"
>
> But the module approach is probably the way to go. :~)
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]