Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HASH INITIALISATION

Status
Not open for further replies.

naq2

Programmer
Aug 3, 2004
74
FR
A very simple question for you guys, expert of hashes:
Can I do that? (I mean... will it work?)
Code:
my %myHash ;

$myHast{toto}{tata} = "DID YOU SEE THIS?!" ;

print $myHast{toto}{tata} ;
#will this print 'DID YOU SEE THIS?' or do I have to initialise the hash in some other way?

Thanks for your help.
 
It's %myHash, not %myHast. Fix the typo and it'll work.
Code:
#!perl
use strict;
use warnings;
use Data::Dumper;

my %myHash ;

$myHash{toto}{tata} = "DID YOU SEE THIS?!";
my $d = Data::Dumper->new([\%myHash], [qw(*myHash)]);
print $d->Dump;

[b]Output:[/b]
%myHash = (
            'toto' => {
                        'tata' => 'DID YOU SEE THIS?!'
                      }
          );


 
Ok...

This was only an example of my question... so the answer is YES... it should initialise the value correctly.

Thanks.
 
OK...

In the same idea... if I want to add a new array in a hash... can I use the same thing?
Code:
my %myHash ;

push(@myHash{anyArray}, "NEW ELEMENT") ;

And if it doesn't work (coz it seams not to work... I get a compilation error), how should I do something like this?

Thanks for your help.
 
$myHash{anyArray} is an array reference.
push expects an actual array as first argument, not a reference. You need to dereference
$myHash{anyArray} using @{...}.
Code:
#!perl
use strict;
use warnings;
use Data::Dumper;

my %myHash ;

$myHash{anyArray} = [];  [b]#an array ref[/b]
#or: @{$myHash{anyArray}} = (); [b]#dereferenced as actual array[/b]
push(@{$myHash{anyArray}}, "NEW ELEMENT"); [b]#push requires array, not ref[/b]

$myHash{toto}{tata} = "DID YOU SEE THIS?!";

my $d = Data::Dumper->new([\%myHash], [qw(*myHash)]);
print $d->Dump;

[b]Output:[/b]
%myHash = (
            'toto' => {
                        'tata' => 'DID YOU SEE THIS?!'
                      },
            'anyArray' => [
                            'NEW ELEMENT'
                          ]
          );

Check out
perldoc perlreftut
perldoc perldsc
perldoc perllol
perldoc perlref

 
ok... thanks a lot for this explanation.

Just one more question:
Do I really need this line:
Code:
$myHash{anyArray} = [];

Thanks again.
 
No. It's there for illustration purposes.
You could cut and paste the code and actually try it, you know ... [3eyes]
 
euh... yes! great idea! :)

but I'm in a very theoretical mood... I've just writen a 2000 lines program on paper... I'll start typing soon! lol!

Thanks anyway.
 
naq,

y oh y did you write 2000 lines of code on paper, unless you're going to scan it in, of course ...

It isn't a handwriting recogniser is it?

;-) --Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
doh! I didn't though about this one! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top