in reply to Re: Lose first element of hash in hash ..
in thread Lose first element of hash in hash ..

7stud, GrandFather, thank you for the quick reply. I have warning and defined variables, I was just simplifying things. YOu are right, there are no first element in a hash (they are stored in an internal order and format, thanks for pointing that out).
sub initrooms { my (@aa, %things, $b, @ar, $t, $de, $ex, $th, @a); open (RF, "rooms.txt") or die "could not open datafile: $!"; undef $/; @ar = split (/0/, <RF>); $/ = "\n"; foreach (@ar) { ($t, $de, $ex, $th) = split /:/, $_; if ($th eq //) { next; } %things = split /-/, $th; print %things; (all elements present) push @a, { TITLE => $t, TEXT => $de, EXITS => $ex, THI +NGS => {%things} }; } return \@a; } sub lookat { my @i; my ($id, $i, $item); my ($tg, $cr, $aoa) = @_; if (exists ($aoa->[$cr]->{THINGS}->{$tg})) { print color("bold blue"), "\n", $aoa->[$cr]->{THINGS}- +>{$tg}, color("reset"); return; } print "\nHmm .. where?"; }
In the exist line I can find all things except the first one pushed onto the array. So $aoa->[0]->{THINGS}->{bed} does not exist but $aoa->[0]->{THINGS}->{chair} does exist. Very strange! And this repeats itself in all 3 $aoa[]

Replies are listed 'Best First'.
Re^3: Lose first element of hash in hash ..
by GrandFather (Saint) on Mar 11, 2010 at 10:55 UTC

    I was going to reverse engineer your code to deduce the format of the data you didn't bother supplying, but your variable names are simply too awful to make that possible in sensible time. Sorry. You are on your own until you can supply a runable script with sample data that shows your problem.


    True laziness is hard work
      > I was going to reverse engineer your code to deduce the format of the data you didn't bother supplying, but your ... was too ... to make that possible in sensible time. Sorry. You are on your own until you can supply a runable script with sample data that shows your problem.

      Such a perfect answer! Polite but clearly pointing to the problem!

      Do you mind if I cite it as a template in other threads from to time? Trademarks needed? ;-)

      Cheers Rolf

      GrandFather, please do not apologise, I thank you for taking the time to look through my post. TA
Re^3: Lose first element of hash in hash ..
by Svante (Sexton) on Mar 11, 2010 at 12:12 UTC

    I notice some problems that may or may not be connected to the issue at hand:

    1. Instead of globally doing undef $/ and then manually resetting it to a newline, you can do it locally: local $/. By the way, couldn't you just set it to "0" (this is a strange input separator; does the file actually consist of null-terminated strings?)? Further reading: perlvar.
    2. Use telling variable names, e.g. my ($title, $text, $exits, $things). This will save you a lot of time figuring out your own code.
    3. The condition ($th eq //) is a bit odd. Don't you just want to compare with the empty string, ($th eq ""), or match with a regex denoting only whitespace, ($th =~ /^\s*$/)?
    4. It seems like your input for "things" consists of a list of elements in the form "key-value-key-value-key-value". Is that right? You should perhaps show some example input.
    5. Don't use barewords as keys for the hash, use strings like 'title', 'text', 'exit', 'things'.
    6. You seem to intend to create a hash reference for 'things'. This is done with a backslash: ... 'things' => \%things ....
    7. You should close your filehandle when done.
    8. Instead of returning from the function lookat inside the if statement, the clear way of conveying your meaning is to use an else clause.

      While most of the suggestions are good, I take objection with two:

      What's wrong with using barewords as hash keys? The quotes mostly clutter up the syntax, and it's rare that you want a function call inside a hash key access.

      Instead of using global / glob filehandles and closing them manually, I recommend to use lexical filehandles, which will close automatically when the enclosing scope is left.

        OK, I retract the bareword comment; I guess it's a matter of circumstance and personal taste.

        I also always use lexical filehandles, but I habitually close them as soon as I'm done with them. This can be done with an explicit call to close, or by introducing a separate scope. The latter form is especially recommended when locally modifying $/ and such things.

      Dear Svante, thanks for your suggestions, I am happy you take the time like this to help improve my code. TA