Problem with Hashes

  • Thread starter Shashank Khanvilkar
  • Start date
S

Shashank Khanvilkar

Hi,
I have a program fragment like this...

----------------------------------------
%HoH = ();
sub1(\%HoH); #Passing the Hoh by reference...
print "size of hash: " . keys(%HoH) . ".\n";
........


sub1{
my $a = @_;
$a->{A}->{1} = 10;
$a->{B}->{2} = 20;
}


----------------------------------------
Since i am passing the Hoh by reference, the print statement should
display 2. However in my case, it gives size of hash as "0"..
what am i doing wrong?
Any help is appreciated.
Regards
Shashank
 
S

Shashank Khanvilkar

Shashank said:
Hi,
I have a program fragment like this...

----------------------------------------
%HoH = ();
sub1(\%HoH); #Passing the Hoh by reference...
print "size of hash: " . keys(%HoH) . ".\n";
.......


sub1{
my $a = @_;
$a->{A}->{1} = 10;
$a->{B}->{2} = 20;
}


----------------------------------------
Since i am passing the Hoh by reference, the print statement should
display 2. However in my case, it gives size of hash as "0"..
what am i doing wrong?
Any help is appreciated.
Regards
Shashank


Sorry.. I got the problem solved..
the onl;y mistake was

sub1{
my ($a) = @_;
$a->{A}->{1} = 10;
$a->{B}->{2} = 20;
}

thasnks
 
J

John Bokma

daniel said:
hi, am still new to Perl so I mark off threads where i might learn
from. so please forgive my extended this thread.....i am lost as to
how changing my $a to my (a$) resolved your problem.....may i ask
"why?"

my $a = scalar context here
my ($a) = list context here
i was under the impression that you used ( ) to declare mutiple
variables at once, my (a$, b$) ... now can't find what ( ) does here
that made the difference,

@_ in scalar context results in the number of items in it, hence my $a =
@_ puts 1 in it if the sub was called with one parameter.

See above. BTW a$ sounds BASIC to me :-D.
 
S

Sherm Pendley

daniel said:
i was under the impression that you used ( ) to declare mutiple variables
at once, my (a$, b$) ... now can't find what ( ) does here that made the
difference,

The concept here is that, unlike C expressions, Perl expressions can
return a list of values. Some expressions return either a list or a
scalar value depending on the context in which they're used.

The expression "my $a = @foo" evaluates @foo in scalar context, which
(as you probably know) returns the number of elements in @foo.

The expression my ($a) = @foo" evaluates @foo in list context, which
returns a list of the elements in @foo. You can assign the result to
another array, or to a list of scalars.

Note that this is a *very* common idiom that's used to simulate named
arguments:

sub do_stuff {
my ($foo, $bar, $baz) = @_;
...
}

This results in $foo, $bar, and $baz being assigned the first three
elements in @_.

sherm--
 
M

Matt Garrish

daniel kaplan said:
hi sherm,

my apologies, and this sounds like idiocy not letting go, but am having a
problem grapsing here:

in the OP, there was this:

sub1(\%HoH); #Passing the Hoh by reference...

and receieved here within the subroutine:

my $a = @_;

where i can't grasp what i am missing is this...he passed the REFERENCE to
the hash, not the HASH itself....and put it in a SCALAR...where later he
would use that reference as needed...

i am obviosuly missing sometihing here...

You're missing the obvious, I'm afraid. $a is a scalar; @_ is an array. It
can be written as:

my ($a) = @_;

to assign the first value to $a, or as

my $a = $_[0];

Make things any clearer?

Matt
 
S

Sherm Pendley

daniel said:
in the OP, there was this:

sub1(\%HoH); #Passing the Hoh by reference...

and receieved here within the subroutine:

my $a = @_;

Of course you're confused - you're looking at the code that didn't work!

Context is Very Important in Perl. The left hand side of an assignment
determines the context in which the right hand side will be evaluated.

In the expression "$a = @_", the left hand side of the assignment is a
scalar, so the right hand side is evaluated in scalar context. The
result of evaluating an array in scalar context is the number of
elements in the array.

One way to assign the first element of @_ to $a is to force the left
hand side of the expression into list context with parenthesis, like this:

my ($a) = @_;

Now the left hand side of the assignment is a *list*. So @_ is evaluated
in list context, which returns a list of elements in @_.

Now, that's not terribly useful just for one argument - he could just as
well have written it like this:

my $a = $_[0];

Or like this:

my $a = shift;

Where this technique *really* pays off is with subroutines that take
multiple arguments. You can assign them all at once, like this:

sub do_something {
my ($foo, $bar, $baz) = @_;
}

Perl doesn't have named arguments, so you'll see this kind of assignment
used a *lot*.

For another example, let's call split() in list context. Let's say you
want to split a row from a tab delimited file, and assign the value of
each column to a scalar. You can do that easily, with:

my ($foo, $bar, $baz) = split(/\t/, $row);

As an aside, the number of elements in @_ is obviously a plain ol'
integer, not a reference of any kind. If the OP had been using 'strict'
and 'warnings' as he should have been doing, Perl would have complained
when he tried to use $a as a hash reference.

sherm--
 
S

Sherm Pendley

daniel said:
just posted above sherm that i finally got it

Yeah, but I didn't see that until I'd already finished writing my reply.
That's what I get for being a windbag, I guess. ;-)

sherm--
 
T

Tad McClellan

my apologies, and this sounds like idiocy not letting go, but am having a
problem grapsing here:


You've already seen the "Context" section in perldata.pod then?

in the OP, there was this:

sub1(\%HoH); #Passing the Hoh by reference...

and receieved here within the subroutine:

my $a = @_;

where i can't grasp what i am missing is this...he passed the REFERENCE to
the hash, not the HASH itself


So far so good...

....and put it


No he didn't (where "it" is the "reference").

He put the number 1 (the number of elements in the @_ array)...

in a SCALAR


.... into that scalar.

...where later he
would use that reference as needed...

i am obviosuly missing sometihing here...


The argument passing in the call was fine.

The argument grabbing in the sub definition was not fine.
 
A

Arndt Jonasson

Shashank Khanvilkar said:
----------------------------------------
%HoH = ();
sub1(\%HoH); #Passing the Hoh by reference...
print "size of hash: " . keys(%HoH) . ".\n";
.......


sub1{
my $a = @_;
$a->{A}->{1} = 10;
$a->{B}->{2} = 20;
}

The problem has already been solved, and someone also pointed out that
using "use strict" would have helped you, or at any rate pointed out
the error sooner in the execution ("Can't use string ("1") as a HASH
ref while "strict refs" in use at ./prog.pl line whatever.")

I'd like to recommend using the debugger. When a variable has gotten
an obviously wrong value at some point, single-step the program from a
point where you know things are correct and see what happens on the way:

perl -d ./prog.pl

Loading DB routines from perl5db.pl version 1.0402
Emacs support available.

Enter h or `h h' for help.

main::(./prog.pl:5): my %HoH = ();
DB<1> s
main::(./prog.pl:6): sub1(\%HoH); #Passing the Hoh by reference...
DB<1> s
main::sub1(./prog.pl:10): my $a = @_;
DB<1> s
main::sub1(./prog.pl:11): $a->{A}->{1} = 10;
DB<1> p $a
1
DB<2>

Here we see that $a is now 1, and not a reference.

(Letting the program run until it dies, after having included "use
strict", and then see where it stopped and why, doesn't seem to
produce useful information in the debugger, but maybe I'm using it
wrong - I'm new to it.)
 
S

Sherm Pendley

daniel said:
have a variable of 'this kind'?

well don't use it in 'this kind' context and the compiler will let you know,
and boom problem found quickly.....but with Perl you can have @row, $row,
$row[0], and so on and so on and it makes you have to be more digilent as
the programmer....

Context is very important in Perl. Subroutines can find out what context
they were called in with wantarray(), and alter their return value(s)
accordingly. An array used in scalar context has a different value than
one used in list context. Etc.

It can be painful for a C programmer who's accustomed to strict typing,
but context is an idea that's central to Perl and it's well worth learning.

sherm--
 
J

Jürgen Exner

daniel said:
i have to admit, in the past few weeks am totally pleased and
impressed with how much you can do with Perl by typing so
little.....but here is where i miss some of the strictness of C....

You must be kidding. Since when does C have strict typing?
C doesn't even have a data type for text.

jue
 
T

Tassilo v. Parseval

Also sprach Jürgen Exner:
You must be kidding. Since when does C have strict typing?

It has static typing...which could be called strict if there weren't
those casting operators that can be used to morph one type into any
other type, irregardless how much sense that makes.
C doesn't even have a data type for text.

That certainly has nothing to do with typing. It would imply that C++ is
more strictly typed than C just because it has the 'string' datatype.

Tassilo
 
T

Tad McClellan

[snip]
irregardless how much sense that makes.


Hmmm, lessee...

"regardless" would mean "without regard", so "irregardless" must
mean "not without regard", ie. *with* regard! [1]

Sorry... you just hit one of my three "hot buttons" there.[2] :)




[1] Though it is so commonly (mis)used that I wouldn't be surprised
if some dictionaries treat it as if it really was a word.

[2] The other two being: the use of "literal" for emphasis when
it is clearly *not* literal but figurative (e.g. He literally
laughed his head off) and using i.e. when they mean e.g.
 
M

Matt Garrish

Tad McClellan said:
[snip]
irregardless how much sense that makes.


Hmmm, lessee...

"regardless" would mean "without regard", so "irregardless" must
mean "not without regard", ie. *with* regard! [1]

Better get used to it, though, it's not going anywhere:

Irregardless originated in dialectal American speech in the early 20th
century. Its fairly widespread use in speech called it to the attention of
usage commentators as early as 1927. The most frequently repeated remark
about it is that "there is no such word." There is such a word, however. It
is still used primarily in speech, although it can be found from time to
time in edited prose. Its reputation has not risen over the years, and it is
still a long way from general acceptance. Use regardless instead.

Matt
 
A

Alan J. Flavell

[2] The other two being: [...]
using i.e. when they mean e.g.

You can get bonus points for using viz. (in the right place, I mean),
and a double bonus for sc. (hardly ever seen these days, sadly...)

To wit: to woo.
 
T

Tad McClellan

Scott Bryce said:
I could care less.


That's what killfiles are for. Just put me in there if my
posts do not interest you.

Or put "Subject: OT" in there if off-topic posts do not
interest you.
 
T

Tassilo v. Parseval

Also sprach Tad McClellan:
[snip]
irregardless how much sense that makes.


Hmmm, lessee...

"regardless" would mean "without regard", so "irregardless" must
mean "not without regard", ie. *with* regard! [1]

Oh, I slipped on a double negation. It's good you pointed that out. I am
not entirely sure why I used this word...I must have picked it up
somewhere, might have been a Seinfeld episode. It's really bad if you
can no longer trust the English you hear from native speakers.
[2] The other two being: the use of "literal" for emphasis when
it is clearly *not* literal but figurative (e.g. He literally
laughed his head off) and using i.e. when they mean e.g.

The distinction between i.e. and e.g. is basic stuff for anyone who
learnt Latin in school. :)

Tassilo
 
T

Tad McClellan

Scott Bryce said:
My apologies if I was not clear.

You mentioned three of your grammatical pet peeves. I responded with two
of mine.

I was not commenting on the quality of your post.


OIC. ( Oh, I see )
 
T

Tad McClellan

Tassilo v. Parseval said:
It's really bad if you
can no longer trust the English you hear from native speakers.


It's really bad then. :-(



I literally blow my top when someone hits "my buttons"
irregardless of their first language, i.e. using
"irregardless" instead of "regardless".

heh.
 

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

Similar Threads

MultiThreading 1
Hash of Hashes 5
Help with Hash of Hashes 1
Iterating hashes 11
Reg Hash of Hash 3
Deleting duplicate values in hash of hashes 1
Sorting hash of hashes 3
Passing hashes to a function 10

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top