Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: another 'array to hash' question

by Abigail-II (Bishop)
on Dec 10, 2003 at 14:40 UTC ( [id://313724]=note: print w/replies, xml ) Need Help??


in reply to another 'array to hash' question

if I have a variable $name, how do I get the string after the $, that is, 'name' in this case?

That's a silly question. Suppose there was a magical function that just did that. Suppose the magical function is called mushroom. Then you would have:

my $name_of_var = mushroom $name; print $name_of_var; # prints 'name'.
There would be no gain. You could as well have typed:
my $name_of_var = 'name';
after all, if you know which variable to get the name of, you already have the name. It's like phoning someone to ask his/her phone number.

Furthermore, what if the variable is $foo {bar} () -> [3], what should this magical function return?

Abigail

Replies are listed 'Best First'.
Re: Re: another 'array to hash' question
by punkish (Priest) on Dec 10, 2003 at 15:02 UTC
    >>if I have a variable $name, how do I get the string after the
    >>$, that is, 'name' in this case?
    >That's a silly question

    thanks ;-).

    I guess I asked that stemming from my original question about converting an array to a hash with array vars as hash key names. But, mostly the problem stems from my not clearly understanding how to deal with variable variables in perl. I have read a few diatribes here and there (Mark Jason Dominus wrote at length about the evils of variable variables), but don't really know how to avoid, work around, or do better.

    Sure, the way you put it, it is silly... after all, if I know the var's name then why run mushroom against the var to find out its name... however, look at the converse side -- I have to know the var's name to be able to run mushroom against it to find out its name... but what if I didn't know the var's name and wanted to find out? what if the var was a var? What if a robot is dialing a lot of phone numbers for me, and at any given time I want to find out what number I am currently talking to, and I don't have that lcd dial thingy that tells me the number I have dialed?

    Any pointers to reading lucid descriptions of dealing with variable variables in perl would be greatly appreciated.

    Many thanks.

      Any pointers to reading lucid descriptions of dealing with variable variables in perl would be greatly appreciated.
      In short, you don't. Why is it that so many people want to use symbolic references in Perl (because that's what it is)? You don't have symbolic references in C. You don't have symbolic references in Java. You don't have symbolic references in many, many languages. And I don't get the impression people programming in Java or C regulary ask how to get the name of a variable, or how to use a value as a variable.
      but what if I didn't know the var's name and wanted to find out?
      How could you? How can you have a variable, and not have its name?
      what if the var was a var?
      What else could a var be than a var? A rose is a rose is a rose is a rose.

      Note also that if you do

      @array = ($var1, $var2, $var3);
      then the elements of the array contain (copies of) the values pointed to by $var1, $var2 and $var3.

      Abigail

        >>Any pointers to reading lucid descriptions of dealing
        >>with variable variables in perl would be greatly appreciated.

        >In short, you don't.

        why not? Because it is bad? Because it is bad style? Because it will lead to unexpected results? I don't mean to be argumentative, but just telling me I shouldn't is not satifying.

        >Why is it that so many people want to
        >use symbolic references in Perl (because that's what it is)?
        >You don't have symbolic references in C. You don't have
        >symbolic references in Java. You don't have symbolic
        >references in many, many languages. And I don't get the
        >impression people programming in Java or C regulary ask
        >how to get the name of a variable, or how to use a value
        >as a variable.

        I know nothing about C and Java so I have no idea what those users do. However, I can think of various uses for such a facility. Suppose I have a choice of actions I can pick from

        $action can be 'dothis' or 'dothat' or 'dosomethingelse'

        And I want to run the functions dothis() or dothat() or dosomething() based on the action picked. I can use the following code --
        &dothis if ($action eq 'dothis'); &dothat if ($action eq 'dothat'); &dosomethingelse if ($action eq 'dosomethingelse');
        or I could just
        &$action;
        but I can't because that is not allowed under use strict; so I
        eval($action);
        and that works and is a lot shorter than the series of if statements. Imagine if I had a lot of choices for action -- that would be a lot of if statements.

        Well, this is just one example.

        >>what if the var was a var?
        >What else could a var be than a var

        Sorry, my bad. I meant, what if the var name was varying and I wanted to do something based on its name.

        Anyway, thanks for the advice. I'll think of better ways to write my code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://313724]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found