Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

% Arrays??

by WhiteBelt (Initiate)
on Jun 06, 2011 at 16:01 UTC ( [id://908314]=perlquestion: print w/replies, xml ) Need Help??

WhiteBelt has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I've been writing some Perl to automate reports and other documents at my workplace. Not terrific at Perl... yet, and so I have a question about a array type declaration. $'s are for regular old variables, @'s are for arrays, O.K. fine and good. In my perl wanderings however, I've come across some sort of command that seems to be an array of some type but that is declared with a % sign.

For instance:

my %seen_before = ();

What sort of thing is this? It seems to be very useful from all the code snippets I've looked at, but percent signs don't search well on google and I'm coming up short trying to figure out just what a my %something is.

Thanks in advance!

Replies are listed 'Best First'.
Re: % Arrays??
by davido (Cardinal) on Jun 06, 2011 at 16:45 UTC

    In fact, a Perl hash is an efficient implementation of what some languages (as well as Computer Science as a discipline) often refer to as an associative array There happens to be a good wikipedia article on that search term, and though it doesn't discuss Perl-specific implementations, it does a good job of explaining the principle.

    Something else associative arrays are good for is representing sparse arrays (with the caveat that Perl hashes don't maintain any particular order).

    But they excel at several things: First, very efficient lookups (In Big-O terms, O(1) lookups). Second, enforced uniqueness of keys (which comes in handy for categorizing, eliminating duplicates, checking for 'seen', etc.).


    Dave

Re: % Arrays??
by toolic (Bishop) on Jun 06, 2011 at 16:07 UTC
Re: % Arrays??
by ikegami (Patriarch) on Jun 06, 2011 at 16:08 UTC
    Hashes are a lot like arrays, except they are keyed by strings instead of numbers.
    $a[5] = ...; # Array $h{abc} = ...; # Hash

    keys returns a list of the keys of the hash's elements.

Re: % Arrays??
by tinita (Parson) on Jun 07, 2011 at 09:20 UTC
    Not terrific at Perl... yet
    did you know that there is a lot of documentation delivered with your perl? and it doesn't even cost anything. except some time to read it, of course.
    anyone learning perl should at least read perlintro. there you will find your strange % arrays (called hashes).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://908314]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found