Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hash keys to scalar variable conversion.

Reply
Thread Tools

Hash keys to scalar variable conversion.

 
 
Derek Basch
Guest
Posts: n/a
 
      05-18-2006
If I have a hash that has many keys/values is there a way to make the
keys into scalars named the same as the key values?

for instance:

$hash{ 'user_name' } = 'bob';

turns into:

$user_name = 'bob'

Thanks,
Derek basch

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-18-2006
"Derek Basch" <(E-Mail Removed)> wrote in news:1147984901.692976.160400
@j33g2000cwa.googlegroups.com:

> If I have a hash that has many keys/values is there a way to make the
> keys into scalars named the same as the key values?
>
> for instance:
>
> $hash{ 'user_name' } = 'bob';
>
> turns into:
>
> $user_name = 'bob'


This is a FAQ.

perldoc -q "variable name"

Found in C:\opt\Perl\lib\pod\perlfaq7.pod
How can I use a variable as a variable name?
Beginners often think they want to have a variable contain the name
of a variable.

In short, this is a bad idea.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-18-2006
Derek Basch wrote:
> If I have a hash that has many keys/values is there a way to make the
> keys into scalars named the same as the key values?
>
> for instance:
>
> $hash{ 'user_name' } = 'bob';
>
> turns into:
>
> $user_name = 'bob'


Why on earth do you think you want to do this?

See also:
perldoc -q "variable name"

Paul Lalli

 
Reply With Quote
 
jdamon@gmail.com
Guest
Posts: n/a
 
      05-18-2006
Hi Derek,

Since the advent of multi-dimensional datastructures with Perl 5, the
Perl community has frowned upon such an action. It used to be the case
that this is how you would construct a large multi-level object, by
doing something like
$a = "b";
$$a = 22;

print $b . "\n";

would print "22";

With that being said, coupled with the fact that in Perl there should
be many ways to do things ( and I would never be the first to judge
someone wanting to try something out ) , you can do the following:


${"main::user_name"} = "bob";

so, it could be done with

foreach ( keys %hash ) {
${"main::$_"} = $hash{$_};
}


This is a way to get around problems when you "use strict" which
would prevent you from using the first soluition.

Hope this helps,

-Jimi

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-18-2006
"(E-Mail Removed)" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) ups.com:

> Since the advent of multi-dimensional datastructures with Perl 5, the
> Perl community has frowned upon such an action.


What action? Please quote properly when you reply. Please read the posting
guidelines for this group.

<snip discussion of symrefs>

It is just harmful to teach bad habits to inexperienced programmers.

http://perl.plover.com/varvarname.html

http://perl.plover.com/varvarname2.html

http://perl.plover.com/varvarname3.html

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
jdamon@gmail.com
Guest
Posts: n/a
 
      05-18-2006
Dude, stop being a tool.

jeez, I don't have to quote what is common wisdom and written in both
Programming Perl and the Perl Cookbook.

Get a life

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-18-2006
>>>>> "jc" == jdamon@gmail com <(E-Mail Removed)> writes:

jc> Dude, stop being a tool.
jc> jeez, I don't have to quote what is common wisdom and written in both
jc> Programming Perl and the Perl Cookbook.

then why did you actually show how to use symrefs if you also advocate
not using them? two regulars admonished the OP for such a poor idea and
then you go ahead and tell him how to do it. do you think he will listen
to the others and figure out a better design remove this concept from
his head? or will he just take the easy route and use your answer and go
down the highway to hellish code?

jc> Get a life

get a brane.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Derek Basch
Guest
Posts: n/a
 
      05-18-2006
Good lord people. Calm the hell down. It's just programming.

I figured out on my own that it was a bad idea and am just using hash
references now.

Thanks for all the help everyone!

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-18-2006
"(E-Mail Removed)" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> Dude, stop being a tool.


Who should stop being a tool? Please quote properly when you post a reply.

....

> Get a life


Who should get a life? Please quote properly when you post a reply.

http://www.google.com/search?q=googl...ps+quote+reply

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-19-2006
>>>>> "DB" == Derek Basch <(E-Mail Removed)> writes:

DB> Good lord people. Calm the hell down. It's just programming.
DB> I figured out on my own that it was a bad idea and am just using hash
DB> references now.

when you have been coding for 30 years and don't see any need to head
off bad coding advice, then i will listen to you and calm down. many of
the world's problems are from bad coding (or bad people in general).

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
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#values and Hash#keys order Alex Fenton Ruby 1 04-15-2006 05:45 AM
No Keys, nor other hash methods on multidimensional hash Xeno Campanoli Ruby 16 08-25-2005 07:04 AM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments