Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using map to assign var values from hash

Reply
Thread Tools

Using map to assign var values from hash

 
 
Richard A. DeVenezia
Guest
Posts: n/a
 
      10-12-2003
This doesn't assign variables. Then commented out 'eval join' version does.
Why doesn't the first version work ?

#!perl -w
use strict;

doStuff (a=>1, b=>2, c=>'foo', foo=>'bar', z=>9);

sub doStuff {
my %param = @_;
my ($a, $b, $c);
my @allowed = qw (a b c);

no strict 'refs';
map $$_ = $param{$_}, @allowed;
use strict;

# eval join ("", map "\$$_ = \$param{'$_'};", @allowed);

print "a=$a\nb=$b\nc=$c\n";
}

--
Richard


 
Reply With Quote
 
 
 
 
Wolfgang Fischer
Guest
Posts: n/a
 
      10-12-2003
On Sun, 12 Oct 2003 04:42:53 -0400, Richard A. DeVenezia wrote:

> This doesn't assign variables. Then commented out 'eval join' version does.
> Why doesn't the first version work ?
>
> #!perl -w
> use strict;
>
> doStuff (a=>1, b=>2, c=>'foo', foo=>'bar', z=>9);
>
> sub doStuff {
> my %param = @_;
> my ($a, $b, $c);
> my @allowed = qw (a b c);
>
> no strict 'refs';
> map $$_ = $param{$_}, @allowed;

Hello,
you can't use variables like $$_; You have to specify the variable name
at compile time, not at run time.
> use strict;
>
> # eval join ("", map "\$$_ = \$param{'$_'};", @allowed);

This creates and evals the string
"$a=$param{'a'};$b=$param{'b'};$c=$param{'c'}; "
So eval doesn't get something like $$_=.... But why don't you use
this?
my($a,$b,$c)=($param{'a'},$param{'b'},$param{'c'}) ;
>
> print "a=$a\nb=$b\nc=$c\n";
> }


 
Reply With Quote
 
 
 
 
Jay Tilton
Guest
Posts: n/a
 
      10-12-2003
Wolfgang Fischer <(E-Mail Removed)> wrote:

: On Sun, 12 Oct 2003 04:42:53 -0400, Richard A. DeVenezia wrote:
:
: > This doesn't assign variables. Then commented out 'eval join' version does.
: > Why doesn't the first version work ?
: >
: > #!perl -w
: > use strict;
: >
: > doStuff (a=>1, b=>2, c=>'foo', foo=>'bar', z=>9);
: >
: > sub doStuff {
: > my %param = @_;
: > my ($a, $b, $c);
: > my @allowed = qw (a b c);
: >
: > no strict 'refs';
: > map $$_ = $param{$_}, @allowed;
:
: you can't use variables like $$_;

Sure you can.

: You have to specify the variable name
: at compile time, not at run time.

Not true at all.

What the OP is trying to do is to assign a value to a scalar by symbolic
reference. It's working, but on the package variables $::a, $::b, and
$::c, not the lexically declared $a, $b and $c he expects.

: But why don't you use
: this?
: my($a,$b,$c)=($param{'a'},$param{'b'},$param{'c'}) ;

Solid advice. Or even,

my($a, $b, $c) = @param{qw/a b c/};

 
Reply With Quote
 
Greg Bacon
Guest
Posts: n/a
 
      10-12-2003
In article <bmb475$kr5ja$(E-Mail Removed)-berlin.de>,
Richard A. DeVenezia <(E-Mail Removed)> wrote:

: This doesn't assign variables. Then commented out 'eval join' version
: does. Why doesn't the first version work ?
: [...]

Symbolic references point to globals, not lexicals, but declaring $a,
$b, and $c as lexicals in that scope made a little more work necessary
to get at the globals. Look at the globals after your assignment:

#! /usr/local/bin/perl

use warnings;
use strict;

our($a,$b,$c);

doStuff (a=>1, b=>2, c=>'foo', foo=>'bar', z=>9);

sub doStuff {
my %param = @_;
# my ($a, $b, $c);
my @allowed = qw (a b c);

no strict 'refs';
map $$_ = $param{$_}, @allowed;
use strict;

# eval join ("", map "\$$_ = \$param{'$_'};", @allowed);

print "a=$a\nb=$b\nc=$c\n";
}

Hope this helps,
Greg
--
. . . this one single rule, rationally construed and applied, is enough to
form the starting point of all that is necessary in government: to make no
more laws than those useful for preventing a man or body of men from
infringing on the rights of other men. -- Walt Whitman, "Government"
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      10-12-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Wolfgang Fischer <(E-Mail Removed)> wrote in
news(E-Mail Removed):

> On Sun, 12 Oct 2003 04:42:53 -0400, Richard A. DeVenezia wrote:
>


>> no strict 'refs';
>> map $$_ = $param{$_}, @allowed;

> Hello,
> you can't use variables like $$_; You have to specify the variable
> name at compile time, not at run time.


Please do not post answers to questions unless you know what you're talking
about. One most certainly can specify variable names at runtime. $$_ is
perfectly valid Perl.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP4lhOmPeouIeTNHoEQLJ5gCcCypVbYabgVI/h9708V412VbAUhAAoNTo
Jq8LkmwsOPzR8iGIfrigG07A
=wYlV
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      10-12-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Richard A. DeVenezia" <(E-Mail Removed)> wrote in
news:bmb475$kr5ja$(E-Mail Removed)-berlin.de:

> This doesn't assign variables. Then commented out 'eval join' version
> does. Why doesn't the first version work ?
>
> #!perl -w
> use strict;
>
> doStuff (a=>1, b=>2, c=>'foo', foo=>'bar', z=>9);
>
> sub doStuff {
> my %param = @_;
> my ($a, $b, $c);
> my @allowed = qw (a b c);
>
> no strict 'refs';
> map $$_ = $param{$_}, @allowed;
> use strict;
>
> # eval join ("", map "\$$_ = \$param{'$_'};", @allowed);
>
> print "a=$a\nb=$b\nc=$c\n";
> }
>


Jay Tilton and Greg Bacon have already pointed out what's going on here,
so I won't repeat what they said.

However, a word of style: Why not leave the caller's value assignments in
a hash, instead of polluting the global variable space? Something like:

$caller_options{$_} = $param{$_} for @allowed;

Without knowing the big picture of what you're doing, I can't say if this
is better than assigning global variables or not. But it's a thought.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP4lieWPeouIeTNHoEQKsIwCgmZFNxSr/lGy0kLbInmGU4SBFz7YAnjCU
Bs9KTxiWvjPLznNEvoOBxprk
=k07R
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Malcolm Dew-Jones
Guest
Posts: n/a
 
      10-12-2003
Eric J. Roode ((E-Mail Removed)) wrote:
: Wolfgang Fischer <(E-Mail Removed)> wrote in

: > you can't use variables like $$_; You have to specify the variable
: > name at compile time, not at run time.

: Please do not post answers to questions unless you know what you're talking
: about.

I think we can safely assume that he thought he knew what he was talking
about, so your advice wouldn't have made any difference.

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-13-2003
Malcolm Dew-Jones <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Eric J. Roode ((E-Mail Removed)) wrote:
> : Wolfgang Fischer <(E-Mail Removed)> wrote in
>
> : > you can't use variables like $$_; You have to specify the variable
> : > name at compile time, not at run time.
>
> : Please do not post answers to questions unless you know what you're talking
> : about.
>
> I think we can safely assume that he thought he knew what he was talking
> about, so your advice wouldn't have made any difference.


Wouldn't have, in the past. It may make a difference in the future.

Anno
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      10-13-2003
Also sprach Anno Siegel:

> Malcolm Dew-Jones <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> Eric J. Roode ((E-Mail Removed)) wrote:
>> : Please do not post answers to questions unless you know what you're talking
>> : about.
>>
>> I think we can safely assume that he thought he knew what he was talking
>> about, so your advice wouldn't have made any difference.

>
> Wouldn't have, in the past. It may make a difference in the future.


But it's not required. People shouldn't deliberately state wrong things,
but then they shouldn't be forced to double-check that their given
advice is 100% correct either. Their mistake is going to be pointed out
to them which is a good way of learning. In my first year in this group
I learnt the most by being corrected.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Richard A. DeVenezia
Guest
Posts: n/a
 
      10-14-2003
Thanks for all the great advice,
I went with Jay's suggestion.

--
Richard


 
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
Bit check using function check(var,pos) ((var)&(1<<pos)) prati C Programming 0 10-27-2012 05:25 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
how to put the content of one hash map to another hash map navS C++ 3 05-09-2008 12:52 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
how access to hash which contains hash and sclar var? kenneth Perl Misc 1 12-11-2004 03:30 PM



Advertisments