Re: Hash return value

2006-03-17 Thread Chris Wagner
Yeah that's what I started to suspect after I posted.  mkhash() should be
saved to a variable name and that variable used in the while each loop.

At 01:18 PM 3/18/2006 +1100, Sisyphus wrote:
>That's not what I find.
>The following script (suggested by Chris) simply prints "All done" for me.
>


my($key, $value);
%temp = mkhash();
while (($key, $value)= each( %temp ))

>my($key, $value);
>while (($key, $value)= each( %{mkhash()} ))
>{
>print("$key = $value\n");
>}
>
>print "All done\n";
>
>sub mkhash
>{
> my %hash=('one'=>1, 'two'=>2, 'three'=>3);
> return %hash;
>}
>
>__END__



three = 3
one = 1
two = 2
All done





--
REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
"...ne cede malis"

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Hash return value

2006-03-17 Thread Sisyphus

- Original Message - 
From: "Eric Amick"
.
.
> You can return a reference to a
> hash instead and then dereference it in the caller:
>
> while (($key, $value)= each( %{ mkhash() }))
> {
> print("$key = $value\n");
> }
>
> sub mkhash
> {
>  my %hash=('one'=>1, 'two'=>2, 'three'=>3);
>  return \%hash;
> }

That's not what I find.
The following script (suggested by Chris) simply prints "All done" for me.

use warnings;

my($key, $value);
while (($key, $value)= each( %{mkhash()} ))
{
print("$key = $value\n");
}

print "All done\n";

sub mkhash
{
 my %hash=('one'=>1, 'two'=>2, 'three'=>3);
 return %hash;
}

__END__

Run it under strictures and I get the fatal error:

Can't use string ("3/8") as a HASH ref while "strict refs" in use.

If I change mkhash() so that it returns a hashref (as you've suggested
above), I just get (ad infinitum):

three = 3
three = 3
three = 3
three = 3
three = 3
three = 3
three = 3
three = 3
three = 3
three = 3
.
.

Looks to me that all you've succeeded in doing is find a way around the
safeguard that was put in place - and that neverending ouptut is pretty much
what I would expect to see when that safeguard has been breached. At each
iteration of the while loop, mkhash() gets re-run and each() gets a new
anonymous hash to deal with.

Cheers,
Rob


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Hash return value

2006-03-17 Thread Eric Amick
On Fri, 17 Mar 2006 12:05:07 -0800, you wrote:

># Using subroutine that returns a hash doesn't work
>while (($key, $value)= each( mkhash() ))
>{
>print("$key = $value\n");
>}
>
>sub mkhash
>{
> my %hash=('one'=>1, 'two'=>2, 'three'=>3);
> return %hash;
>}

The each() function expects its argument to start with %. As perlsub
explains, your function actually returns a list, not a hash; arrays and
hashes are "flattened" when returned from a function, just as they're
flattened when passed to a function. You can return a reference to a
hash instead and then dereference it in the caller:

while (($key, $value)= each( %{ mkhash() }))
{
print("$key = $value\n");
}

sub mkhash
{
 my %hash=('one'=>1, 'two'=>2, 'three'=>3);
 return \%hash;
}
-- 
Eric Amick
Columbia, MD
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Hash return value

2006-03-17 Thread Chris Wagner
At 04:40 PM 3/16/2006 -0700, Edwards, Mark (CXO) wrote:
>The each function requires a hash as an argument.  I would think that a
>subroutine that returns a hash could be used as the argument, but it
>doesn't work.  Why?  Is my syntax wrong or is that just the way Larry
>made it?

U have to evaluate it in hash context.  This should work.
 
while (($key, $value)= each( %{&mkhash()} )) {
print("$key = $value\n");
}







--
REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
"...ne cede malis"

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Hash return value

2006-03-16 Thread Sisyphus

- Original Message - 
From: "Edwards, Mark (CXO)"
.
.
> # Using subroutine that returns a hash doesn't work
> while (($key, $value)= each( mkhash() ))

One problem with allowing that is that at each iteration of the while loop,
mkhash() gets re-run, returning another anonymous hash. The each() function
can deal with only one hash at a time - and yet it would be seeing a
separate additional anonymous hash at each iteration of the while loop.

(What would happen if mkhash() returned *different* hashes at each
iteration.)

I think it may also be that each() needs a *named* hash so that it "can keep
track of where it's up to" so to speak . not sure about that.

Cheers,
Rob

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Hash return value

2006-03-16 Thread Edwards, Mark (CXO)
The each function requires a hash as an argument.  I would think that a
subroutine that returns a hash could be used as the argument, but it
doesn't work.  Why?  Is my syntax wrong or is that just the way Larry
made it?
 
use warnings;
use strict;

my ($key, $value, %hash) ;

# Using hash for function argument works
%hash=mkhash();
while (($key, $value)= each( %hash ))
{
print("$key = $value\n");
}


# Using subroutine that returns a hash doesn't work
while (($key, $value)= each( mkhash() ))
{
print("$key = $value\n");
}

sub mkhash
{
 my %hash=('one'=>1, 'two'=>2, 'three'=>3);
 return %hash;
}

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs