RE: reference-question

daneman (daneman@planet.nl)
Fri, 29 Dec 2000 23:15:59 +0100


Thanks for your reply Scott. I thought I had figured out how to use
references having read Perlref and perlreftut, but I probably haven't.

(BTW: You're right that the question itself doesn't involve libwww, but the
script in wich I wanted to use the discussed lines explicitly does, wich
explains why I (obvliviously) posted it to the libwww-list)

Thank, Daneman

>-----Original Message-----
>From: Scott Gifford [mailto:sgifford@tir.com]
>Sent: Thursday, December 28, 2000 2:57 AM
>To: daneman
>Subject: Re: reference-question
>
>
>You will probably have better luck asking in one of the Perl
>newsgroups; this list is just for discussing the LWP Perl modules, and
>you don't seem to be using those at all . . .
>
>But, since I already hit reply, see some comments below.
>
>"daneman" <daneman@planet.nl> writes:
>
>> Hello,
>>
>> I hope I can make my problem clear:
>>
>> I have these 4 lines (as an example):
>>
>>
>$files{'file1'}=['http://www.f1a.com','http://www.f1b.com','http://
>www.f1c.c
>> om'];
>>
>$files{'file2'}=['http://www.f2a.com','http://www.f2b.com','http://
>www.f2c.c
>> om'];
>> $dir{'somedir'}=\%files;
>> $links[xx]=\%dir;
>>
>> By doing this I attempt to make a 4 dimensional list of links from an
>> (unknown) number of files within an (unknown) number of directories in an
>> (unknown) number of directory-levels.
>> This way I could point to any of the links in the list with:
>>
>> $somelink = $links[dirlevel]{dirname}{filename}[linknumber];
>>
>> My questions are:
>>
>> When I say:
>>
>> print @{$links};
>>
>> nothing happens. I don't get an errormessage but there's nothing
>printed on
>> the screen neither.
>> Supposing the rest of the script is ok (contenttype etc), what do I do
>> wrong?
>
>When you create $links[xx], Perl creates a list called @links, and
>then puts your data at the xx'th item of this list.  When you say
>@{$links}, you look at the scalar value $links (which doesn't exist),
>and interpret it as a list reference.  Since $links doesn't exist
>(@links does), it comes out as an empty list.
>
>You'll want to use either:
>
>    $links[xx]=\%dir;
>    print @links;
>
>or
>
>    $links->[xx]=\%dir;
>    print @{$links};
>
>The second of these puts a reference to a list in $links, then puts a
>value in the list that $links references, so your original print
>statement is correct.
>
>> I also failed to get the values of '%dir':
>>
>> foreach $key (%{$dir})
>> 	{
>> 	print "$key: ".${$dir}{$key};
>> 	}
>
>Same as above; you either need to do
>
>    $dir{'somedir'}=\%files;
>    foreach $key (%dir)
>        {
>        print "$key: ".${$dir}{$key};
>        }
>
>or
>
>    $dir->{'somedir'}=\%files;
>    foreach $key (%{$dir})
>        {
>        print "$key: ".${$dir}{$key};
>        }
>
>
>
>> In general my question is: how do I get the values of each of
>the indexes of
>> @links?
>
>Your only problem is references; see the perlref(1) manpage (or the
>Camel book) for more information.
>
>Good luck,
>
>----ScottG.
>