On Wed, Sep 01, 1999 at 09:43:03PM -0400, Charles Heffner wrote:
> Hi,
>       To make sure I understand, I'm spitting back my
> interpretation of your tutorial. Perl I know not...
> 
> > Hashes are also sometimes called "associative arrays" - so they are
> > kind of like arrays. Except that the data that's stored in a
> > `regular' array is referenced by a number whereas the data stored in
> > a hash is referenced by an arbitrary string (the key).
>       So, e.g., listing some or all of the days of the week is an
> array ('Monday' 'Tuesday' 'Wednesday' 'etc.'), and one of
> those days is a string. And, all values in an array must be
> sequentially arranged from an arbitrary start point.
> (1,2,3,4... M,T,W... -5,-10,-15...)

Correct so far. With Perl you would write
$week[0]="Monday", $week[1]="Tuesday", etc.
(never mind the "$" instead of the "@" in front of week - that's
because $week[x] refers to a single entry of the array @week and
that_'s a string...)

Or you could write @week = ("Monday", "Tuesday", ...)

With a hash on the other hand you would write
$names{"John"}="John Smith", $names{"Bill"}="Bill Meyer", etc.
Again, with Perl there are other ways, like e.g.

%names = (
    "John"  => "John Smith",
    "Bill"  => "Bill Meyes"
)

> > E.g if you have an array @ARRAY ("@" is Perl way to say `this is an
> > array') you could ask what is the third member of @ARRAY or you could
> > put a string in the first position of @ARRAY. On the other hand, if
> > you have a hash %HASH ("%" again is the Perl way to say `this is a
> > hash') you would ask what data is associated with the key
> > "AnyString". The keys of a hash have no particular order.
>       Ok, 1st, hashes do not have to be sequentially arranged
> like arrays. 2nd, hashes are interpretations of arrays.
> E.g., the array Week has values Mo,Tu,We,Th,Fr,Sa,Su. So,
> %Tdays{'@Week'}=Tu,Th, where Tdays is the hash, Week is the
> array, and Tu,Th are the keys?

Your right on the 1st point but I wouldn't say that hashes are an
interpretation of arrays. The internal representation is totaly
different. On the other hand, in Perl hashes can often be treated
as arrays.
I'm not realy sure what you are trying to do in your example. You
cannot use an array as the key for a hash. If "Tu" and "Th" are keys
for the hash %Tdays, then you would get to the data that's
associated with those keys by using $Tdays{"Tu"} or $Tdays{"Th"}.
(see above for why I say _$_Tdays and not _%_Tdays)

> > So what does the selectkeys handler do? Simple! You have a hash with
> > lots of different data associated with different keys and you need
> > only the small part of that data that's associated with a few select
> > keys.
>       So if I want to extract the key(s) Tu,Th, from array @Week,
> I define the filter like this:

An array doesn't have keys. Therefore the selectkeys handler only
works on hashes. You have to use the map handler if you have an
array with lots of hashes with common keys. (for an example see
below)

> <filter name=selectkeys keys=Tu,Th> to retrieve all links on
> my target page matching this key, or invert-esly, to prevent
> any strings from my target page matching that to be
> retrieved.

I'll try an example to clarify what I mean:

Let's say you have a data structure where you have an array that
contains lots of headlines from a certain news site. Each headline
consist of a title, an url and a description. In Perl this could be
written like this (where {...} stands for an `anonymous' hash - that
is a hash that doesn't belong to any variable):

@headlines = (
    {
        "title"   => "They are here!",
        "url"     => "http://incredible.news/story1.html",
        "desc"    => "A very long description..."
    },
    {
        "title"   => "Another amazing story",
        "url"     => "http://incredible.news/story2.html",
        "desc"    => "You get the idea..."
    },
    ...
)

If you have an array like this but need only the title and url of
each story you would use the selectkeys handler like this:
<filter name=selectkeys keys="title,url">

>       If I'm lost, please correct me. I am good at logic, but
> pretty slow on the uptake of new languages.

I think you are on the right way! ;-)

Sorry if this post got too long.

bye,
Jan

-- 
Jan Peter Hecking - University of Rostock - Dept. of Computer Science
eMail: [EMAIL PROTECTED]      (icq #14643959)  /\/\ 
public key: http://www.informatik.uni-rostock.de/~jhecking/pgp  \--/ 
fingerprint: 18 91 55 54 62 67 CC 71   05 5F C7 AE E4 F3 B7 D9   \/  

-
If you would like to unsubscribe from this mailing list send an email to 
[EMAIL PROTECTED] with the body "unsubscribe newsclipperlist 
YOUR_EMAIL_ADDRESS" (without the quotes) or use the form provided at 
http://www.NewsClipper.com/TechSup.htm#MailingList.

Reply via email to