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

UPDATED MY POST. how do I access it from sub two?
#!/usr/bin/perl -w use strict; system('clear'); my $test = first(); two($test); sub first{ my @data; my %objREF; $data[2] = 'hi there'; $objREF{poker} = \$data[2]; } sub two{ my $objREF = shift; #print "$objREF{'poker'}"; #print "$$objREF{'poker'}"; #print "$objREF->{'poker'}"; #print "$$objREF->{'poker'}"; #print "$$$objREF{'poker'}"; #print "$$$$objREF{'poker'}"; #print "${$objREF{'poker'}}\n"; print "$${$objREF->{'poker'}}\n"; }
### original post ###
I have read that linked lists in perl are slow but I can't quite find an example of one. Is this a linked list? If so how can I change it without losing any of the functionality?
#!/usr/bin/perl -w use strict; system('clear'); my @data; $data[2] = 'hi there'; my %objREF; $objREF{poker} = \$data[2]; print "struct example\n"; print "======================================\n"; print "original value:\n"; print_me(); print "change by array value:\n"; $data[2] = 'joker'; print_me(); print "change value by ref:\n"; ${$objREF{poker}} = 'batman'; print_me(); print "change the array\n"; $data[2] = 'robin'; print_me(); sub print_me{ print "array is: $data[2]\n"; print "Ref is: ${$objREF{poker}}\n\n"; }

Edit by castaway - restored original content

Replies are listed 'Best First'.
Re: code sample: possible improvements
by davido (Cardinal) on Jan 10, 2005 at 03:44 UTC

    No, you have not created a linked list. You've created a single-element hash, holding a reference to an element of an array named @data. The element being referenced is $data[2].

    So to answer your question, no, that is not a linked list. As for how you can change it? I don't know.... what do you have in mind? I mean change it in what way? What kind of change are you after? Do you need it to be a linked list? If so, will there be additional elements, and how are you adding them, etc?

    Basically, I guess in order to help you more, you're going to have to go ahead and give us some idea of what it is you want to do so we dont' have to guess.


    Dave

      I was told it was a linked list.... I didnt think it was. So I thought I would ask here. I just know I dont know everything : )

        Well, there's no list there. A basic linked list is a list where one element contains data, as well as a pointer to the next element. The next element contains data, and a pointer to the next element, and so on.

        If you're really interested in linked lists, most textbooks on Computer Science discuss them at one point or another. If you're interested in implementing a linked list in Perl, I recommend the book, Mastering Algorithms with Perl (the Wolf book), published by O'Reilly & Associates. It has a chapter devoted to linked lists, and gives a pretty good discussion of other types of complex datastructures too. It's not really a beginning Perl book. If basic Perl is what you're looking for, start with reading Perl's own POD. For example, perlreftut, and perldsc discuss datastructures and references. But if you're kind of beyond that level and ready for more then try the algorithms book, it's pretty interesting.


        Dave

Re: code sample: possible improvements
by Errto (Vicar) on Jan 10, 2005 at 04:50 UTC

    I cannot answer your question, because you have changed your post so that it no longer says what you are looking for. Moreover, any monk who reads your question after this point will have the same problem. Updating your posts with further comments and corrections is fine. Updating your post to delete the question you originally asked is not fine, so please don't do it.

    Nevertheless I will guess that your question concerns why the code as you have it now does not print out "hi there" and moreover returns an error Not a HASH reference at - line 27. The reason is that the value returned by first is not a reference to the hash %objREF but rather a reference to a scalar value, containing a string, which so happens to be the third element of an array that has fallen out of scope. If you wrote your code for two as

    my $objREF = shift; print $$objREF;
    you would see the desired result. If you added the line
    return \%objREF;
    to the end of first then the line
    print ${$objREF->{poker}}
    would be the correct way to print it out from two.

    Update: This reply refers to the modified version of the node, before the original was restored, but I am leaving it in place for safe-keeping. Thanks castaway and janitors.

Re: code sample: possible improvements
by NetWallah (Canon) on Jan 10, 2005 at 01:43 UTC
    This is NOT a linked list, so I would have no fear of losing functionality, since there isn't much to start with, so feel free to change it.

    Let us know if you have a specific question you need help with.

        ..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld

      no that was the question. If it was a linked list I was at a loss on how to do it differant. Thanks for the info.