For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > ref / hash









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ref / hash
Brent Clark

2005-08-02, 5:00 pm

Hi all

I my thanks to all whom have tried to help me earlier, but alas, I still
cant seem to get this right.
I know this gonna kick me because it will be a stupid small mistake.

my ($ref_hash, $fileName) = @_;

foreach my $roomCode ( keys %{ $ref_hash->{$fileName} }){

foreach my $rmData ( keys %{ $ref_hash->{$fileName}->{$roomCode} } ){
print $ref_hash->{$fileName}->{$roomCode}->{$rmData}; #Nothing
gets displayed
}

print does not display anything.

Here is my Data::Dumper info

$VAR1 = {
'Grac01' => {
'StndRm' => 'Standard Room',
'Suite' => 'Suite',
'Pent' => 'Penthouse'
},

If anyone could be so kind to help, it would greatfully be appreciated.

Kind Regards
Brent Clark
JupiterHost.Net

2005-08-02, 5:00 pm



Brent Clark wrote:

> Hi all
>
> I my thanks to all whom have tried to help me earlier, but alas, I still
> cant seem to get this right.
> I know this gonna kick me because it will be a stupid small mistake.
>
> my ($ref_hash, $fileName) = @_;
>
> foreach my $roomCode ( keys %{ $ref_hash->{$fileName} }){
>
> foreach my $rmData ( keys %{ $ref_hash->{$fileName}->{$roomCode}
> } ){
> print
> $ref_hash->{$fileName}->{$roomCode}->{$rmData}; #Nothing
> gets displayed
> }
>
> print does not display anything.


Because $ref_hash->{$fileName}->{$roomCode} is the string you want and
not a hashref:


my $ref_hash = {
'Grac01' => {
'StndRm' => 'Standard Room',
'Suite' => 'Suite',
'Pent' => 'Penthouse'
},
};

for(keys %{ $ref_hash->{'Grac01'} }) {
print "$ref_hash->{'Grac01'}->{$_}\n";
}

John Doe

2005-08-02, 5:00 pm

Brent Clark am Dienstag, 2. August 2005 14.08:
> Hi all
>
> I my thanks to all whom have tried to help me earlier, but alas, I still
> cant seem to get this right.
> I know this gonna kick me because it will be a stupid small mistake.


use strict; use warnings;

at the beginning of the script would have helped you with something like

Can't use string ("Standard Room") as a HASH ref while
"strict refs" in use at ./test11.pl line 15.

As I didn't realize in my last answer, you imagine an inexistent additional
level in your data structure.

> my ($ref_hash, $fileName) = @_;
>
> foreach my $roomCode ( keys %{ $ref_hash->{$fileName} }){
>
> foreach my $rmData ( keys %{ $ref_hash->{$fileName}->{$roomCode} } ){
> print $ref_hash->{$fileName}->{$roomCode}->{$rmData}; #Nothing
> gets displayed
> }
>
> print does not display anything.
>
> Here is my Data::Dumper info
>
> $VAR1 = {
> 'Grac01' => {
> 'StndRm' => 'Standard Room',
> 'Suite' => 'Suite',
> 'Pent' => 'Penthouse'
> },
>
> If anyone could be so kind to help, it would greatfully be appreciated.


---- script ---

#!/usr/bin/perl
use strict; use warnings;
my $struct= {
'Grac01' => {
'StndRm' => 'Standard Room',
'Suite' => 'Suite',
'Pent' => 'Penthouse'
}
};

sub enum {
my ($ref_hash, $fileName) = @_;
foreach my $roomCode ( keys %{ $ref_hash->{$fileName} }){
print $ref_hash->{$fileName}->{$roomCode}, "\n";
}
}

enum ($struct, 'Grac01');

---- script end ---

This prints:

Standard Room
Penthouse
Suite



joe
John W. Krahn

2005-08-02, 5:00 pm

Brent Clark wrote:
> Hi all


Hello,

> I my thanks to all whom have tried to help me earlier, but alas, I still
> cant seem to get this right.
> I know this gonna kick me because it will be a stupid small mistake.
>
> my ($ref_hash, $fileName) = @_;
>
> foreach my $roomCode ( keys %{ $ref_hash->{$fileName} }){
>
> foreach my $rmData ( keys %{ $ref_hash->{$fileName}->{$roomCode}
> } ){
> print
> $ref_hash->{$fileName}->{$roomCode}->{$rmData}; #Nothing
> gets displayed
> }
>
> print does not display anything.
>
> Here is my Data::Dumper info
>
> $VAR1 = {
> 'Grac01' => {
> 'StndRm' => 'Standard Room',
> 'Suite' => 'Suite',
> 'Pent' => 'Penthouse'
> },
>
> If anyone could be so kind to help, it would greatfully be appreciated.


$ perl -e'
my $ref_hash = {
Grac01 => {
StndRm => "Standard Room",
Suite => "Suite",
Pent => "Penthouse",
},
};
my $fileName = "Grac01";

for my $roomCode ( keys %{ $ref_hash->{ $fileName } } ) {
print "$roomCode\t$ref_hash->{$fileName}{$roomCode}\n";
}
'
StndRm Standard Room
Pent Penthouse
Suite Suite



John
--
use Perl;
program
fulfillment
Brent Clark

2005-08-02, 5:00 pm

Hi List

A big thanks to all.

I really appreciate your assistance in this.

Kind Regards and a big thanks again.

Brent Clark
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com