sending scalar and hash to function

2011-06-27 Thread buzon
I am trying to send an scalar and hash to a function, but is not  
receiving the value of the hash


My code is:

  dosomething('option',{'extraparam1'=>'hello'});

  function dosomething
{
($myopt,%myparams) = @_;
print "opt = $myopt\n";
while( my ($k, $v) = each %myparams )
  { print "$k = $v \n"; }
}

And result is:

  opt = option
  HASH(0xcb4f490) =
  HASH(0xcb4f490) =


I have tryied different solutions (look below) but no one  
loads/display the values of %myparams:


1) function dosomething
{
($myopt,%myparams) = @_;
print "opt = $myopt\n";
while( my ($k, $v) = each %{$myparams} )
  { print "$k = $v \n"; }
}

2) function dosomething
{
$myopt = shift;
%myparams = shift;
print "opt = $myopt\n";
while( my ($k, $v) = each %$myparams )
  { print "$k = $v \n"; }
}

3) function dosomething
{
use Tie::RefHash;
($myopt,%myparams) = @_;
tie %hash_postparams, "Tie::RefHash";
print "opt = $myopt\n";
while( my ($k, $v) = each %myparams )
  { print "$k = $v \n"; }
}

Any idea what I am doing wrong?



Really thank you in advance,

Alejandro


--
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/




sending scalar and hash to function (note)

2011-06-27 Thread buzon

of course that is

  sub dosomething

but still not working.


--
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/




sending scalar and hash to function [SUMMARY]

2011-06-27 Thread buzon


Found the solution (my apologies).

I am receiving both an scalar, the second one as a reference, then it  
must be assigned to an other var.


  sub dosomething
{
($myopt,$myparams) = @_;
%myparams = $myparams;
print "opt = $myopt\n";
while( my ($k, $v) = each %myparams )
  { print "$k = $v \n"; }
}



--
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/




Re: sending scalar and hash to function [SUMMARY]

2011-06-27 Thread Mike Williams
On Mon, Jun 27, 2011 at 2:44 PM,  wrote:

>
> Found the solution (my apologies).
>
> I am receiving both an scalar, the second one as a reference, then it must
> be assigned to an other var.
>
> No.  It does not have to be assigned to another var.  Instead you should
dereference the reference.

 sub dosomething
>{
>($myopt,$myparams) = @_;
>## %myparams = $myparams;# skip the asssignment and creation oa a
> new hash
>print "opt = $myopt\n";
>

    while( my ($k, $v) = each %myparams )
   while( my ($k, $v) = each %{$myparams} )   # dereference the
reference with %{$ref}

>  { print "$k = $v \n"; }
>}
>
>
The assignment you had assigns the hash reference to a key of the hash you
created, with nothing assigned as a value.

It is a real, real bad idea (for many reasons) to create variables of
different types with the same names.

A major benefit of passing a reference is that you only move one item, the
reference, instead of the entire hash.  It doesn't make a lot of difference
in this case, but if you had a hash with thousands of key/value pairs
passing a reference and using that reference uses just a few bytes of
memory.  Copying the entire hash uses many thousands of bytes.

Happy hacking,

Mike


Re: sending scalar and hash to function

2011-06-27 Thread Shaun Fryer
In the first code snippit, your function will receive a hash reference,
which is a scalar. If you get rid of the curly braces, it will work. Better
still would be

my ($myopt, $myparams) =@_;

...and...

each %$myparams

--
Shaun Fryer
1-647-723-2729
On Jun 27, 2011 2:33 PM,  wrote:
> I am trying to send an scalar and hash to a function, but is not
> receiving the value of the hash
>
> My code is:
>
> dosomething('option',{'extraparam1'=>'hello'});
>
> function dosomething
> {
> ($myopt,%myparams) = @_;
> print "opt = $myopt\n";
> while( my ($k, $v) = each %myparams )
> { print "$k = $v \n"; }
> }
>
> And result is:
>
> opt = option
> HASH(0xcb4f490) =
> HASH(0xcb4f490) =
>
>
> I have tryied different solutions (look below) but no one
> loads/display the values of %myparams:
>
> 1) function dosomething
> {
> ($myopt,%myparams) = @_;
> print "opt = $myopt\n";
> while( my ($k, $v) = each %{$myparams} )
> { print "$k = $v \n"; }
> }
>
> 2) function dosomething
> {
> $myopt = shift;
> %myparams = shift;
> print "opt = $myopt\n";
> while( my ($k, $v) = each %$myparams )
> { print "$k = $v \n"; }
> }
>
> 3) function dosomething
> {
> use Tie::RefHash;
> ($myopt,%myparams) = @_;
> tie %hash_postparams, "Tie::RefHash";
> print "opt = $myopt\n";
> while( my ($k, $v) = each %myparams )
> { print "$k = $v \n"; }
> }
>
> Any idea what I am doing wrong?
>
>
>
> Really thank you in advance,
>
> Alejandro
>
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
> For additional commands, e-mail: beginners-cgi-h...@perl.org
> http://learn.perl.org/
>
>