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

So I just started using Perl, and I had to write a database for a website. I still don't know a lot about Perl, but with what I knew the best way to do it was with a bunch of hashes. That way I had links, and the categories of links all organized neatly. I accomplished this by creating pointers which pointer to other pointers and so on until they pointed to hashes ( a sort of hash of hashes of hashes etc.)

I am tired of manually updating this database and have endeavored into writing an editing perl script. I am stuck on how to edit the actual data structure. Here is a general idea of how it looks like and my problem
$A = { B => { 1 => bla, 2 => bla, 3 => bla }, C => null, D => { 52 => bla, 53 => bla } }


My problem is encountered when I try to change "C" and make it point to not a value but a hash or list. I know have to reinitialize C => {} then $A->{$C}->{$23} = bla. To do my add method, I take in two parameters, the location which is $a->{$c} and the key,data set ($23, bla.) I want to make this dynamic so if $a originally points to null, I want to be able to take the location that was passed in, $a->{$c}, and try to make that key data set, if I can't make it check if $c points to a hash or value, if it doesn't exist, go back to $a and see if that points to a hash and if it doesn't make it point to a hash, then i will be able to add the desired data.

I have the idea in my head, I just can't figure out a way to go backwards through the references (ie given a final location break that location up and go from the front, or take the final location apart part by part from the back)

I am about to resort to passing in the location as a string, breaking up the string, and getting breaking it up that way. Sorry if this is confusing I tried to simplify it out because at this point my data structure is pretty ... pointee.

Replies are listed 'Best First'.
Re: Going Backwards In Reference
by clinton (Priest) on Jul 12, 2007 at 18:56 UTC
    I'm afraid I didn't follow your explanation of what you're trying to do. Perhaps an example would help?

    But some things that I think you might have been asking:

    • A key or a value in a hash doesn't point back to its containing hash.

    • If  $a->{C} == 'value'; but you want $a->{C} instead to contain a hash ref, you can do this:

      my %C = ( d => 1, e => 2); $a{C} = \%C; OR just: $a{C} = { d => 1, e => 2};

    If that wasn't what you were asking, I apologise - please explain further.

    Clint

      Clint, thanks for answering. I am actually going to do exactly what you said, however, I am trying to find where it points to a value so I can reinit it into a hash. I ended up doing it in strings, and its working fine now, I take it step by step and if at any point it points to a value it simply gets replaced by a hash as needed. I am little stuck on the following code perhaps you could help me out.
      sub addTo() { my $location = $_[0]; my $data = $_[1]; my @keys = split(/\//, $location); my $key; my $current = $database; while($key = shift(@keys)) { $current->{$key} = {} unless (exists $current->{$key} && ref($curr +ent->{$key}) eq "HASH"); $current = $current->{$key}; } #I AM STUCK HERE ----- $current = $data; }


      Since $current is a pointer, I understand that by setting it to $data it doesn't change the value of what its referencing but changes $current to equal the scalar value $data.

      I tried the following snippet
      my $pointer = \$database->{"2002"}->{"Gustav"}->{"General"}->{"Overvie +w"}; ${$pointer} = "nasdfaull";
      This worked well, and actually changed the value it was referencing. However, when I tried to do the same referencing technique below with $current = $database, I would get stuck on the unless line, telling me that I didn't have a hash reference. The addTo method definitely works if I keep it as it now but switch the $current = $data code with
      $current->{$key} = $data;
      I would really like to make it work without having to switch that though.

      -Constantin

        There is a bit of nomenclature here that is not working in your favor. Perl does not have a concept of "pointers", although it does use "references" a lot (there is a not so subtle distinction - in Perl objects are reference counted). Perl allows references to pretty much anything, including references so you can have what may be considered indirect references, but they still ain't pointers.

        I suspect a good dose of reading is what you most need at this point. Take a look at perldata and perllol. You may find the Tutorials section Data Types and Variables helps a lot too.

        Assignment always replaces whatever was previously contained by a variable. It doesn't matter what "type" of thing the variable previously contained, it gets replaced.


        DWIM is Perl's answer to Gödel