Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl hash of hash efficiency.

Reply
Thread Tools

Perl hash of hash efficiency.

 
 
tak
Guest
Posts: n/a
 
      08-02-2006
Hi,

I have a script, that loads a txt file, with 240k lines in it to a hash
currently. And when it loads the data to the hash - it becomes slower
and slower when it reaches may be around 150k (probably due to
collision, since perl's hash uses linear chaininig...).

So, i am thinking of implementing it in hash of hash... using the first
letter of these records, and divide them into 26 sub hashes. (Since
these records' first letter are pretty random from A-Z).

So, I tried it, the performance is about the same... why??

The difference between what i had, and what i am doing now is,

Before:
$mainHash{$somekey} = \@values;

After:
my $letter = substr $values, 0 , 1;
$mainHash{$letter}{$somekey} = \@values;

Shouldnt the "after" format helps the memory usage / efficiency by
alot? B/c it lowered the collision by 26 times...???

Thanks,
T

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-02-2006
"tak" <> wrote:
> Hi,
>
> I have a script, that loads a txt file, with 240k lines in it to a hash
> currently. And when it loads the data to the hash - it becomes slower
> and slower when it reaches may be around 150k


How much memory do you have? How much are you using at this point?

> (probably due to
> collision, since perl's hash uses linear chaininig...).


That is a rather unlikely bit of speculation, especially on a modern Perl.
How many buckets does your hash have and use? (print scalar %hash).

> So, i am thinking of implementing it in hash of hash... using the first
> letter of these records, and divide them into 26 sub hashes. (Since
> these records' first letter are pretty random from A-Z).
>
> So, I tried it, the performance is about the same... why??


Solving the imagined problem rarely solves the real problem. (And if that
were the problem it was that easy to fix, don't you think Perl would
already have made that fix itself in the core hashing code?)

When the facts don't fit your theory, re-examing your theory. You probably
have a swapping problem, not a hash collision problem. And if you do have
a collision problem, the better way to fix it would be to start out with a
higher number of buckets, by assigning to the keys function.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
tak
Guest
Posts: n/a
 
      08-02-2006

wrote:
> "tak" <> wrote:
> > Hi,
> >
> > I have a script, that loads a txt file, with 240k lines in it to a hash
> > currently. And when it loads the data to the hash - it becomes slower
> > and slower when it reaches may be around 150k

>
> How much memory do you have? How much are you using at this point?


>From looking at the PF Usage - it is about 1.9 GB. on a 1gb machine -

the available physical memory are down to about 10 MB when loading...
But the CPU usage remains about 5% only...


>
> > (probably due to
> > collision, since perl's hash uses linear chaininig...).

>
> That is a rather unlikely bit of speculation, especially on a modern Perl.
> How many buckets does your hash have and use? (print scalar %hash).
>


I have 1 main_hash, which stores 27 hashes in it. And out of each 27
hashes, it averages about 9k unique strings. print scalar %hash
reports, 23/32. What does this number mean?


> > So, i am thinking of implementing it in hash of hash... using the first
> > letter of these records, and divide them into 26 sub hashes. (Since
> > these records' first letter are pretty random from A-Z).
> >
> > So, I tried it, the performance is about the same... why??

>
> Solving the imagined problem rarely solves the real problem. (And if that
> were the problem it was that easy to fix, don't you think Perl would
> already have made that fix itself in the core hashing code?)
>


So, what do you suggest?


> When the facts don't fit your theory, re-examing your theory. You probably
> have a swapping problem, not a hash collision problem. And if you do have
> a collision problem, the better way to fix it would be to start out with a
> higher number of buckets, by assigning to the keys function.
>


Can you elaborate on what you mean by a swapping problem? And I thought
about assigning higher number of bucket to the hash itself , but i
cannot find the related function to set that... I am a Java programmer,
and this is my first perl script.. I tried looking into the constructor
for the hash itself, but it doesnt seem like it accepts argument...?

Last question,

How Do you delete an element within a hoh? Say i have a hash of hash,
like the following.

my %hoh();

loop() { # say this is the loop of each line of my txtFile
my $value = "TheRecordFromMyTxtFile";
my $letter = substr $value, 0, 1; # say, i am using the first letter
as the key for subhash.
my $myKey = substr $value, 5, 9; # Say position 5 - 9 is the key for
the element.
$hoh{$letter}{$myKey} = $value
}


Now, I want to delete a particular value from one of the subhash...

I tried doing this,

delete $hoh{$letter}{$value};

But it doesnt seem like it is deleting... B/c if I try to get the
length of the $hoh{$letter}, it still reports the same number...


Thanks!
tak



> Xho
>
> --
> -------------------- http://NewsReader.Com/ --------------------
> Usenet Newsgroup Service $9.95/Month 30GB


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-02-2006
tak wrote:
> wrote:
> > "tak" <> wrote:

> I have 1 main_hash, which stores 27 hashes in it. And out of each 27
> hashes, it averages about 9k unique strings. print scalar %hash
> reports, 23/32. What does this number mean?


It means that Perl has allocated 32 buckets for this hash, and that 23
of them are currently in use. So, only 4 collissions in the "main"
hash.

> > And if you do have
> > a collision problem, the better way to fix it would be to start out with a
> > higher number of buckets, by assigning to the keys function.
> >

>
> And I thought
> about assigning higher number of bucket to the hash itself , but i
> cannot find the related function to set that...


Xho just gave it to you. The `keys` function. If you read the Perl
documentation on this function, by typing at your console window:
perldoc -f keys
you will find:
====================
As an lvalue "keys" allows you to increase the
number of hash buckets allocated for the given hash.
This can gain you a measure of efficiency if you
know the hash is going to get big. (This is similar
to pre-extending an array by assigning a larger
number to $#array.) If you say

keys %hash = 200;

then %hash will have at least 200 buckets allocated
for it--256 of them, in fact, since it rounds up to
the next power of two.
====================
> I am a Java programmer, and this is my first perl script..


Welcome to the world of Perl. You'll love it, I promise.

> I tried looking into the constructor for the hash itself,


There is no such thing. Constructors are methods of classes. Hashes
are native data types, not objects. They are simply declared and used.

> but it doesnt seem like it accepts argument...?


I don't know what you were trying to give arguments to. If you mean
simply the `my` keyword, then you are correct - you cannot pre-allocate
buckets when you declare the hash. Instead, declare it, and then
assign buckets, using the keys function as described above.

> How Do you delete an element within a hoh? Say i have a hash of hash,
> like the following.
>
> my %hoh();
>
> loop() { # say this is the loop of each line of my txtFile


I don't know what this means. This is not Perl code. Please show real
code whenever possible.

> my $value = "TheRecordFromMyTxtFile";
> my $letter = substr $value, 0, 1; # say, i am using the first letter
> as the key for subhash.
> my $myKey = substr $value, 5, 9; # Say position 5 - 9 is the key for
> the element.
> $hoh{$letter}{$myKey} = $value
> }
>
>
> Now, I want to delete a particular value from one of the subhash...
>
> I tried doing this,
>
> delete $hoh{$letter}{$value};


That is precisely how you delete that particular value from that
particular "subhash".

> But it doesnt seem like it is deleting... B/c if I try to get the
> length of the $hoh{$letter}, it still reports the same number...


What, exactly, do you mean by "get the length of the $hoh{$letter}"?
Again, please show real code whenever possible. Did you, by any
chance, do something like:
print length $hoh{$letter};
?

That does not, at all, give you what you want. When you use a
reference in a scalar context, you get a string representing the type
of reference and it's memory address. To see what I mean, try printing
out:
print $hoh{$letter};
You should see something like
HASH(0x14d410)
It is this string that you were passing to the length function.
Obviously, the length of that string isn't going to change simply
because you removed one of the key/value pairs.
To determine the "size" (that is, number of keys) of a hash, you again
use the keys function, this time in sclar context:
print scalar(keys %{$hoh{$letter}});

The additional punctuation around $hoh{$letter} is what we call
"dereferencing" a reference. You can read all about it by typing at
your console window:
perldoc perlreftut
perldoc perllol
perldoc perldsc

Hope this helps,
Paul Lalli

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-02-2006
"tak" <> wrote:
> wrote:
> > "tak" <> wrote:
> > > Hi,
> > >
> > > I have a script, that loads a txt file, with 240k lines in it to a
> > > hash currently. And when it loads the data to the hash - it becomes
> > > slower and slower when it reaches may be around 150k

> >
> > How much memory do you have? How much are you using at this point?

>
> >From looking at the PF Usage - it is about 1.9 GB. on a 1gb machine -

> the available physical memory are down to about 10 MB when loading...
> But the CPU usage remains about 5% only...


If I read this right, you are using 1.9GB of virtual memory but you only
have 1.0GB of RAM, and so have swapped out 900MB? And you have only 10MB
of *free* memory? That means you are probably swapping like crazy, and
thus the CPU load is low as it spends most of the time waiting for disk.

> >
> > > (probably due to
> > > collision, since perl's hash uses linear chaininig...).

> >
> > That is a rather unlikely bit of speculation, especially on a modern
> > Perl. How many buckets does your hash have and use? (print scalar
> > %hash).
> >

>
> I have 1 main_hash, which stores 27 hashes in it. And out of each 27
> hashes, it averages about 9k unique strings. print scalar %hash
> reports, 23/32. What does this number mean?


There are 32 buckets, of which 23 have at least one value. So there is
either 1 five-way collision, or 4 two-way collisions, or somewhere in
between. But that doesn't really tell you much. I meant for you to get
this number on the one-big-hash implementation, before you changed over to
the hash of hashes.

> > > So, i am thinking of implementing it in hash of hash... using the
> > > first letter of these records, and divide them into 26 sub hashes.
> > > (Since these records' first letter are pretty random from A-Z).
> > >
> > > So, I tried it, the performance is about the same... why??

> >
> > Solving the imagined problem rarely solves the real problem. (And if
> > that were the problem it was that easy to fix, don't you think Perl
> > would already have made that fix itself in the core hashing code?)
> >

>
> So, what do you suggest?


Identifying the real problem first

Right now, I'd say that that is memory. So you could try to find a more
memory efficient way to hold your data. Or make a multi-pass approach.
Or maybe tie the hash to disk explicitly. Or use a database to hold the
data. (Or buy more memory)


>
> > When the facts don't fit your theory, re-examing your theory. You
> > probably have a swapping problem, not a hash collision problem. And if
> > you do have a collision problem, the better way to fix it would be to
> > start out with a higher number of buckets, by assigning to the keys
> > function.
> >

>
> Can you elaborate on what you mean by a swapping problem?


Your OS has a virtual memory system that lets you use more memory than
you actually have, by swapping/paging out "unused" memory to disk. But it
can get very, very slow if you are actively using more pages than fit in
real memory.

(Paul answered the rest of your questions)

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
tak
Guest
Posts: n/a
 
      08-02-2006

Paul Lalli wrote:
> tak wrote:
> > wrote:
> > > "tak" <> wrote:

> > I have 1 main_hash, which stores 27 hashes in it. And out of each 27
> > hashes, it averages about 9k unique strings. print scalar %hash
> > reports, 23/32. What does this number mean?

>
> It means that Perl has allocated 32 buckets for this hash, and that 23
> of them are currently in use. So, only 4 collissions in the "main"
> hash.


Why 4 collision? Do you mean 32 - 23 = 9?? or b/c you knew that i have
27 subhashes, so 27 - 23?? Without using the 27 hash of hashes, print
scalar %mainHash reports 16k / 23k. (Of course - it reported the
number, but I didnt take them down, just remember its 16k and 23k) That
is a hugh amount of collision.


>
> > > And if you do have
> > > a collision problem, the better way to fix it would be to start out with a
> > > higher number of buckets, by assigning to the keys function.
> > >

> >
> > And I thought
> > about assigning higher number of bucket to the hash itself , but i
> > cannot find the related function to set that...

>
> Xho just gave it to you. The `keys` function. If you read the Perl
> documentation on this function, by typing at your console window:
> perldoc -f keys
> you will find:
> ====================
> As an lvalue "keys" allows you to increase the
> number of hash buckets allocated for the given hash.
> This can gain you a measure of efficiency if you
> know the hash is going to get big. (This is similar
> to pre-extending an array by assigning a larger
> number to $#array.) If you say
>
> keys %hash = 200;
>
> then %hash will have at least 200 buckets allocated
> for it--256 of them, in fact, since it rounds up to
> the next power of two.
> ====================
> > I am a Java programmer, and this is my first perl script..

>
> Welcome to the world of Perl. You'll love it, I promise.
>
> > I tried looking into the constructor for the hash itself,

>
> There is no such thing. Constructors are methods of classes. Hashes
> are native data types, not objects. They are simply declared and used.
>
> > but it doesnt seem like it accepts argument...?

>
> I don't know what you were trying to give arguments to. If you mean
> simply the `my` keyword, then you are correct - you cannot pre-allocate
> buckets when you declare the hash. Instead, declare it, and then
> assign buckets, using the keys function as described above.



I tried to do this, keys %main_hash = 300000; but it is still running
slow when it reaches 150000... perhaps it is not the collision problem,
as xho mentioned?



>
> > How Do you delete an element within a hoh? Say i have a hash of hash,
> > like the following.
> >
> > my %hoh();
> >
> > loop() { # say this is the loop of each line of my txtFile

>
> I don't know what this means. This is not Perl code. Please show real
> code whenever possible.
>
> > my $value = "TheRecordFromMyTxtFile";
> > my $letter = substr $value, 0, 1; # say, i am using the first letter
> > as the key for subhash.
> > my $myKey = substr $value, 5, 9; # Say position 5 - 9 is the key for
> > the element.
> > $hoh{$letter}{$myKey} = $value
> > }
> >
> >
> > Now, I want to delete a particular value from one of the subhash...
> >
> > I tried doing this,
> >
> > delete $hoh{$letter}{$value};

>
> That is precisely how you delete that particular value from that
> particular "subhash".
>


Say I have the key of subhash, as $letter, and the item in subhash as,
$value.

delete $hoh{$letter}{$value};

This should delete that from the hash, right?




> > But it doesnt seem like it is deleting... B/c if I try to get the
> > length of the $hoh{$letter}, it still reports the same number...

>
> What, exactly, do you mean by "get the length of the $hoh{$letter}"?
> Again, please show real code whenever possible. Did you, by any
> chance, do something like:
> print length $hoh{$letter};
> ?
>
> That does not, at all, give you what you want. When you use a
> reference in a scalar context, you get a string representing the type
> of reference and it's memory address. To see what I mean, try printing
> out:
> print $hoh{$letter};
> You should see something like
> HASH(0x14d410)
> It is this string that you were passing to the length function.
> Obviously, the length of that string isn't going to change simply
> because you removed one of the key/value pairs.
> To determine the "size" (that is, number of keys) of a hash, you again
> use the keys function, this time in sclar context:
> print scalar(keys %{$hoh{$letter}});
>
> The additional punctuation around $hoh{$letter} is what we call
> "dereferencing" a reference. You can read all about it by typing at
> your console window:
> perldoc perlreftut
> perldoc perllol
> perldoc perldsc
>



Say I want to look at the value of in this key --
$hoh{$letter}{$value}, how do you print it?
I tried, print $hoh{$letter}{$value}; - but it prints nothing....

Thanks,
Tak


> Hope this helps,
> Paul Lalli


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      08-02-2006

Quoth "tak" <>:
>
> wrote:
> > "tak" <> wrote:
> > > Hi,
> > >
> > > I have a script, that loads a txt file, with 240k lines in it to a hash
> > > currently. And when it loads the data to the hash - it becomes slower
> > > and slower when it reaches may be around 150k

> >
> > How much memory do you have? How much are you using at this point?

>
> >From looking at the PF Usage - it is about 1.9 GB. on a 1gb machine -

> the available physical memory are down to about 10 MB when loading...
> But the CPU usage remains about 5% only...


So, you are thrashing. You've run out of memory: I would suggest using
one of the DBM modules, probably DB_File. This stores the contents of
the hash in a (structured, binary, fast-to-index) file on disk, which
will probably make things faster.

> > > (probably due to
> > > collision, since perl's hash uses linear chaininig...).

> >
> > That is a rather unlikely bit of speculation, especially on a modern Perl.
> > How many buckets does your hash have and use? (print scalar %hash).

>
> I have 1 main_hash, which stores 27 hashes in it. And out of each 27
> hashes, it averages about 9k unique strings. print scalar %hash
> reports, 23/32. What does this number mean?


From perldoc perldata:

| If you evaluate a hash in scalar context, it returns false if the hash
| is empty. If there are any key/value pairs, it returns true; more
| precisely, the value returned is a string consisting of the number of
| used buckets and the number of allocated buckets, separated by a slash.
| This is pretty much useful only to find out whether Perl's internal
| hashing algorithm is performing poorly on your data set. For example,
| you stick 10,000 things in a hash, but evaluating %HASH in scalar
| context reveals "1/16", which means only one out of sixteen buckets has
| been touched, and presumably contains all 10,000 of your items. This
| isn't supposed to happen.

(Note that this is not meant as a rebuke: noone can be expected to have
all the arcana in Perl's std docs memorized. It is meant so that you may
remember where to find it next time . )

So, your main hash is using 23 buckets to store your 27 subhashes... not
such a useful thing to know . The real question is, how many buckets
does your original hash (with all the data in it) use? For instance, on
my perl

my %h;

for (1..240_000) {
$h{$_} = 1;
}

print scalar %h;

prints '157199/262144', so the hash is using 157199 buckets, and each
bucket has on average 240000/157199 ~~ 1.5 entries in it, which should
not be a problem.

> > When the facts don't fit your theory, re-examing your theory. You probably
> > have a swapping problem, not a hash collision problem. And if you do have
> > a collision problem, the better way to fix it would be to start out with a
> > higher number of buckets, by assigning to the keys function.
> >

>
> Can you elaborate on what you mean by a swapping problem?


Your system has started thrashing: the working set (the pages in current
use) has exceeded the size of physical memory, and the system is
spending all its time swapping things in and out.

> And I thought
> about assigning higher number of bucket to the hash itself , but i
> cannot find the related function to set that... I am a Java programmer,
> and this is my first perl script.. I tried looking into the constructor
> for the hash itself, but it doesnt seem like it accepts argument...?


The next para after my previous quote:

| You can preallocate space for a hash by assigning to the keys()
| function. This rounds up the allocated buckets to the next power of two:
|
| keys(%users) = 1000; # allocate 1024 buckets

> Last question,
>
> How Do you delete an element within a hoh? Say i have a hash of hash,
> like the following.
>
> my %hoh();


Did you even try this? Perl Is Not Java: this is a syntax error. You
don't need the parens.

> loop() { # say this is the loop of each line of my txtFile


What is this 'loop()'? Have you been reading about Perl6? Or did you
mean

sub loop {

?

> my $value = "TheRecordFromMyTxtFile";


(You really want to sort out your indentation. Makes life easier for
both you and us.)

> my $letter = substr $value, 0, 1; # say, i am using the first letter
> as the key for subhash.
> my $myKey = substr $value, 5, 9; # Say position 5 - 9 is the key for
> the element.
> $hoh{$letter}{$myKey} = $value
> }
>
>
> Now, I want to delete a particular value from one of the subhash...
>
> I tried doing this,
>
> delete $hoh{$letter}{$value};


That's correct (assuming $value corresponds to $myKey in the above, not
to $value there: that is, you delete an element by specifying its key).

> But it doesnt seem like it is deleting... B/c if I try to get the
> length of the $hoh{$letter}, it still reports the same number...


You really need to learn some basic Perl. I'd recommend a book:
'Learning Perl' published by O'Reilly is universally recommended as a
good place to start. An alternative would be to read through the
perldocs, but that's not an easy way to learn.

length (see perldoc -f length) treats its argument as a string and
returns the length of that string. $hoh{$letter} contains a hash
*reference*: see perldoc perldsc and perldoc perlreftut for how
multi-level data structures are implemented in Perl. Or, again, a decent
book will cover it. Now, when you stringify a hash ref, you get
something that looks like 'HASH(0x80142180)', which is basically
useless, and is always the same length.

To find the number of keys in a hash, you do as it says in perldoc -f
length: 'scalar keys %hash'. This is somewhat complicated by the fact
that what you have is not a hash but a hash ref, so we apply 'Use Rule
1' from perlreftut:

# an ordinary hash
print scalar keys %hash;

# replace the var name with { }
print scalar keys %{ }

# put the hashref inside the braces
print scalar keys %{ $hoh{$letter} };

Yes, I agree this is a little icky, but that's what you get when you
graft complex data structures onto a language (Perl4) that doesn't
really support them .

A useful tool for examining data structures is the module Data:umper
(obviously, you want to run a test on a smaller dataset rather than
dumping a hash of 240k entries).

Ben

--
All persons, living or dead, are entirely coincidental.
Kurt Vonnegut
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-02-2006
"tak" <> wrote:
>
> Without using the 27 hash of hashes, print
> scalar %mainHash reports 16k / 23k. (Of course - it reported the
> number, but I didnt take them down, just remember its 16k and 23k) That
> is a hugh amount of collision.


How many things where in the hash at the time you did the
print scalar %mainHash? (print scalar keys %mainHash). If it had 150K
things in it at the time, than there are 150K/16K or about 10 entries per
bucket. Higher than I would expect but not aweful.

> >
> > I don't know what you were trying to give arguments to. If you mean
> > simply the `my` keyword, then you are correct - you cannot pre-allocate
> > buckets when you declare the hash. Instead, declare it, and then
> > assign buckets, using the keys function as described above.

>
> I tried to do this, keys %main_hash = 300000; but it is still running
> slow when it reaches 150000... perhaps it is not the collision problem,
> as xho mentioned?


No, it probably isn't collisions that is the problem. But to be sure (but
mostly out of curiousity), what did print scalar %main_hash give you after
you loaded 150000 into it with this pre-allocation?

> > > I tried doing this,
> > >
> > > delete $hoh{$letter}{$value};

> >
> > That is precisely how you delete that particular value from that
> > particular "subhash".
> >

>
> Say I have the key of subhash, as $letter, and the item in subhash as,
> $value.
>
> delete $hoh{$letter}{$value};
>
> This should delete that from the hash, right?


Yes.
....

> Say I want to look at the value of in this key --
> $hoh{$letter}{$value}, how do you print it?
> I tried, print $hoh{$letter}{$value}; - but it prints nothing....


Um, is this before or after you deleted it?

Are you using warnings? If so, did you get an uninitialized value
warning?


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-02-2006
tak wrote:
> Paul Lalli wrote:
> > tak wrote:
> > > wrote:
> > > > "tak" <> wrote:
> > > I have 1 main_hash, which stores 27 hashes in it. And out of each 27
> > > hashes, it averages about 9k unique strings. print scalar %hash
> > > reports, 23/32. What does this number mean?

> >
> > It means that Perl has allocated 32 buckets for this hash, and that 23
> > of them are currently in use. So, only 4 collissions in the "main"
> > hash.

>
> Why 4 collision? Do you mean 32 - 23 = 9?? or b/c you knew that i have
> 27 subhashes, so 27 - 23??


Yes. You have 27 values in your hash. Your hash is using 23 buckets.
Therefore, 4 buckets are being used twice.

> Without using the 27 hash of hashes, print
> scalar %mainHash reports 16k / 23k. (Of course - it reported the
> number, but I didnt take them down, just remember its 16k and 23k) That
> is a hugh amount of collision.


So it would seem.

> I tried to do this, keys %main_hash = 300000; but it is still running
> slow when it reaches 150000... perhaps it is not the collision problem,
> as xho mentioned?


That would be my guess. Again, it seems to be your theory that is
faulty. You assumed that the slowness was caused by collissions. The
facts do not support that theory.

> > > Now, I want to delete a particular value from one of the subhash...
> > >
> > > I tried doing this,
> > >
> > > delete $hoh{$letter}{$value};

> >
> > That is precisely how you delete that particular value from that
> > particular "subhash".

>
> Say I have the key of subhash, as $letter, and the item in subhash as,
> $value.
>
> delete $hoh{$letter}{$value};


Your code does not match your description. In the above $letter is a
key of the *main* hash, and $value is a key in the subhash
%{$hoh{$letter}}.

> This should delete that from the hash, right?


That would delete the key/value pair in the hash %{$hoh{$letter}} which
has the key $value.

> Say I want to look at the value of in this key --
> $hoh{$letter}{$value}, how do you print it?
> I tried, print $hoh{$letter}{$value}; - but it prints nothing....


Then that key does not exist in that hash, or that key's value in the
hash is the empty string (or the undefined value). Are you using
warnings? If so, printing a non-existing element of a hash should give
you a warning. Please enable them if you are not. Again, your
description above is not matching your code.

It's time for you to show some *actual* code, so we can better help
you. Please post a short-but-complete script that demonstrates one or
more of the failures you are encountering.

Also, it would be appreciated if you trimmed your replies down to only
include the relevant bits of quoted material. Thank you.

Paul Lalli

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-02-2006
Ben Morrow wrote:
> Quoth "tak" <>:
> > But it doesnt seem like it is deleting... B/c if I try to get the
> > length of the $hoh{$letter}, it still reports the same number...

>
> You really need to learn some basic Perl. I'd recommend a book:
> 'Learning Perl' published by O'Reilly is universally recommended as a
> good place to start.


> $hoh{$letter} contains a hash
> *reference*: see perldoc perldsc and perldoc perlreftut for how
> multi-level data structures are implemented in Perl. Or, again, a decent
> book will cover it.


Yet, ironically, not the book you recommended. Once the OP has
finished with "Learning Perl", he should probably move on to
"Intermediate Perl", which does cover multi-level data structures.

Paul Lalli

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Perl hash and rehash seeds; deterministic hash ordering ozgune@gmail.com Perl Misc 4 01-22-2007 07:58 PM
Hash of hash in perl Shashank Khanvilkar Perl Misc 3 11-18-2004 08:44 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57