use strict;
use warnings;
use 5.010;
my $th = 'hello-10-world-20';
my %things = split /-/, $th;
while ( my($key, $val) = each %things ) {
say "$key => $val";
}
--output:--
hello => 10
world => 20
What do you consider to be the 'first' key/value pair in a hash?
| [reply] [d/l] |
Always use strictures (use strict; use warnings;). I bet turning on warnings is enlightening.
True laziness is hard work
| [reply] |
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[]
| [reply] [d/l] |
| [reply] |
| [reply] [d/l] [select] |
use Data::Dumper;
print Dumper($th,\%things),"\n";
to your script and enlightenment will follow (probably)
| [reply] [d/l] |
jethro, thanks for suggesting that .. It produced some interesting output!
'THINGS' => {
'pillow' => 'Nice soft pillow ..',
'sheets' => 'Long white sheets .. You slept
+in them last night.
',
'
bed' => 'There is some sheets and a pillow here'
The one that is missing, kind of falls out of the print instead of being listed nicely togetehr with the other ones.
That datadumper is a pretty useful tool! (I learn so much new here everyday :)
| [reply] [d/l] |
Setting $Data::Dumper::Useqq = 1; would make it clearer, but the issue is that the key is "\nbed" (newline-b-e-d)
| [reply] [d/l] [select] |
It seems that the key you are looking for is not stored as 'bed' but as '\nbed'.
| [reply] |