How to insert or append new items into a perl hash ?

G

Googy

Hi,

I can't able to find answers for this basic question, I googled and did
everything possible to find an answer,
There are methods to delete and find the existence of an item in the
hash but not for inserting or appending a new item into perl hash.

Suggestions required.
 
J

John Bokma

Googy said:
Hi,

I can't able to find answers for this basic question, I googled and did
everything possible to find an answer,
There are methods to delete and find the existence of an item in the
hash but not for inserting or appending a new item into perl hash.

my %hash;

$hash{ foo } = 'new item';
$hash{ bar } = 'another new item';

I just inserted two items.

If you want to "append", I guess you mean something like:

$hash{ bar } = ...
$hash{ bar } = ....

in that case the value of the hash needs to be something you can store
more values in than one.

You might want to read the following documentation that comes with perl
(if you don't know how to access it, type perldoc perldoc):

perlreftut
perldsc
perllol
 
H

Henry Law

Googy said:
Hi,

I can't able to find answers for this basic question, I googled and did
everything possible to find an answer,
There are methods to delete and find the existence of an item in the
hash but not for inserting or appending a new item into perl hash.

It's in the part of the Perl documentation called "perldata"; you get
there by issuing perldoc perldata on a command line, or going to
the relevant part of the HTML documentation.

I looked on your behalf, though, and found it not particularly easy to
find the information you want, so here's a quick tutorial. There's more
to it than this (the really good Perl people in this group will be able
to tell you more).

To insert a new item into a hash just give it a value:

$myhash{"newkey"} = "newvalue";
$anotherhash{$the_new_key} = $some_other_variable;

If you already have one of those items in the hash then you'll
over-write it, so you may need to add "... unless exists
$myhash{newkey}" or whatever.

Note the dollar sign, not a "%": a single element of a hash is a scalar.
Maybe you knew that already but it is confusing if you don't.

"Append" has no meaning; hashes are not ordered. (And if you iterate
over them they won't come out in the order you expect, or any other
given order either).
 
G

Googy

John said:
my %hash;

$hash{ foo } = 'new item';
$hash{ bar } = 'another new item';

I just inserted two items.

If you want to "append", I guess you mean something like:

$hash{ bar } = ...
$hash{ bar } = ....

in that case the value of the hash needs to be something you can store
more values in than one.

You might want to read the following documentation that comes with perl
(if you don't know how to access it, type perldoc perldoc):

perlreftut
perldsc
perllol

But when I return this hash form a subroutine, its not maintaining its
structure. Why ?
 
V

vamsee.2005

Henry said:
It's in the part of the Perl documentation called "perldata"; you get
there by issuing perldoc perldata on a command line, or going to
the relevant part of the HTML documentation.

I looked on your behalf, though, and found it not particularly easy to
find the information you want, so here's a quick tutorial. There's more
to it than this (the really good Perl people in this group will be able
to tell you more).

To insert a new item into a hash just give it a value:

$myhash{"newkey"} = "newvalue";
$anotherhash{$the_new_key} = $some_other_variable;

If you already have one of those items in the hash then you'll
over-write it, so you may need to add "... unless exists
$myhash{newkey}" or whatever.

Note the dollar sign, not a "%": a single element of a hash is a scalar.
Maybe you knew that already but it is confusing if you don't.

"Append" has no meaning; hashes are not ordered. (And if you iterate
over them they won't come out in the order you expect, or any other
given order either).


Sorry for my previous dump reply.
 
H

Henry Law

Googy said:
But when I return this hash form a subroutine, its not maintaining its
structure. Why ?

You need to learn to read the documentation, and best of all get some
books or other learning aids. Google for them.

But the answer to your question is found in perldoc perlsub and is this:
subroutines only ever receive and return lists. So your hash is
flattened into a list on the way into and/or out of your subroutine. If
it's a simple {a=>"AA",b=>"BBB",z=>"foo"} hash then you may
coincidentally get away with it, but if it has a more complicated
structure than that it will be lost.

To do what you want you have to use a reference to the hash; too
complicated to explain here with your level of knowledge.
 
K

Keith Keller

But when I return this hash form a subroutine, its not maintaining its
structure. Why ?

When you return which hash from which subroutine? Post real
code! (And read the Posting Guidelines while you're at it.)

Part of your answer is in

perldoc perlsub

In particular look for the word "flattened".

--keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top