[26400] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8571 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 26 06:05:29 2005

Date: Wed, 26 Oct 2005 03:05:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 26 Oct 2005     Volume: 10 Number: 8571

Today's topics:
        FAQ 4.44 How do I test whether two arrays or hashes are <comdog@pair.com>
    Re: Moving data structure around better than globals? <noreply@gunnar.cc>
    Re: Moving data structure around better than globals? <noreply@gunnar.cc>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 26 Oct 2005 10:03:01 +0000 (UTC)
From: PerlFAQ Server <comdog@pair.com>
Subject: FAQ 4.44 How do I test whether two arrays or hashes are equal?
Message-Id: <djnk8k$7a3$1@reader2.panix.com>

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.

--------------------------------------------------------------------

4.44: How do I test whether two arrays or hashes are equal?

    The following code works for single-level arrays. It uses a stringwise
    comparison, and does not distinguish defined versus undefined empty
    strings. Modify if you have other needs.

        $are_equal = compare_arrays(\@frogs, \@toads);

        sub compare_arrays {
            my ($first, $second) = @_;
            no warnings;  # silence spurious -w undef complaints
            return 0 unless @$first == @$second;
            for (my $i = 0; $i < @$first; $i++) {
                return 0 if $first->[$i] ne $second->[$i];
            }
            return 1;
        }

    For multilevel structures, you may wish to use an approach more like
    this one. It uses the CPAN module FreezeThaw:

        use FreezeThaw qw(cmpStr);
        @a = @b = ( "this", "that", [ "more", "stuff" ] );

        printf "a and b contain %s arrays\n",
            cmpStr(\@a, \@b) == 0
                ? "the same"
                : "different";

    This approach also works for comparing hashes. Here we'll demonstrate
    two different answers:

        use FreezeThaw qw(cmpStr cmpStrHard);

        %a = %b = ( "this" => "that", "extra" => [ "more", "stuff" ] );
        $a{EXTRA} = \%b;
        $b{EXTRA} = \%a;

        printf "a and b contain %s hashes\n",
            cmpStr(\%a, \%b) == 0 ? "the same" : "different";

        printf "a and b contain %s hashes\n",
            cmpStrHard(\%a, \%b) == 0 ? "the same" : "different";

    The first reports that both those the hashes contain the same data,
    while the second reports that they do not. Which you prefer is left as
    an exercise to the reader.



--------------------------------------------------------------------

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.

  AUTHOR AND COPYRIGHT

    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights 
    reserved.

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.


------------------------------

Date: Wed, 26 Oct 2005 12:00:47 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Moving data structure around better than globals?
Message-Id: <3s92i7Fmsc9dU1@individual.net>

Tad McClellan wrote:
> Gunnar Hjalmarsson wrote:
>>I have accepted that package globals as well as file scoped lexicals 
>>should be avoided if possible,
> 
> I'd say "if practicable" rather than "if possible".
> 
> It is the _abuse_ of global variables that is often warned against,
> they do have their place.

Yeah, I took it to an extreme this time, contrary to my habit. ;-)

>>I'm passing $d around to just about all the functions, 
> 
> So then, it would hit my "impracticable" limit.  :-)
> 
>>and it struck me 
>>that this approach actually is very similar to using global variables. 
> 
> I very often have a single global hash with "configuration"
> type values in it.
> 
>>So my question is: Why would this be better (if you think it is)?
> 
> I wouldn't say that passing the same arg in every function call
> is better.

Thanks for confirming my feeling.

My "impracticable" approach is well working, though, so I won't change 
it now. But I'll keep this in mind for next time.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Wed, 26 Oct 2005 12:00:52 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Moving data structure around better than globals?
Message-Id: <3s92ibFmsc9dU2@individual.net>

Mothra wrote:
> Gunnar Hjalmarsson wrote:
>>I have accepted that package globals as well as file scoped lexicals
>>should be avoided if possible, and hence a program I'm currently
>>writing does not include any globals.
> 
> (snipped)
> 
> I asked something similar in the past.
> 
>  http://tinyurl.com/8bbel

Thanks for the link. That discussion is absolutly relevant to the topic 
I raised.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8571
***************************************


home help back first fref pref prev next nref lref last post