Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Variable scalar creation.

by snappybo (Initiate)
on Feb 13, 2005 at 05:29 UTC ( #430530=perlquestion: print w/replies, xml ) Need Help??

snappybo has asked for the wisdom of the Perl Monks concerning the following question:

Yes i know evey one says don't do this...
But i just want to know how i can get this to work..

($db->FetchRow()); @names = $db->FieldNames(); @values = $db->Data(); $arraycnt = '0'; while ($names[$arraycnt] ne '') { $$names[$arraycnt] = $values[$arraycnt]; $arraycnt = $arraycnt +1; }

I don't need to know about hashes or arrays, this is the best way to get a result i require without having to recode huge amounts.. Because the code has already been written, just modifcations are being made..

Thanks in advance..

Code tags and formatting added by davido.

Replies are listed 'Best First'.
Re: Variable scalar creation.
by NetWallah (Canon) on Feb 13, 2005 at 05:47 UTC
    Definitely NOT recommended, but if you turn off strict, you can accomplish what you want by:
    my $arraycnt = 0; $$_ = $values[$arraycnt++] for @names;
    However, I would strongly urge you to use hashes - both for safety, and convenience.

    Update: Also, please read Why it's stupid to `use a variable as a variable name'

    Another warning - there is no guarantee that the name you are attempting to create from the content of @names is a valid perl variable-identifier. You may get strange run-time errors.

        ..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld

      Sure i can, i'm just trying to create a set of variables that match the database field names filled with the relevant data (a good 800 or so fields).

      As i said i understand it could be better to use hashes and so on, but in this case the variables built from @names aren't variables at all, because they are fieldnames from a database that don't change..

      Which allows me to use the one reference in all the applications that access this particular database.. And the above worked perfectly.. Thanks..

        And how exactly is using the fieldnames from a database to dynamically create scalar variable names an improvement over using a hash to hold the fieldnames and their respective values? I'm really curious. With the hash method, you get one hash, easily transportable by hard reference, and easily manipulated. With the symbolic ref method, you get variable names that are created in the global symbol table hash, which you must continue to use via soft reference throughout the remainder of the script unless your script both dynamically creates the variable names and hard codes them. ...if it's the latter, and you're hard coding the names at later points in the script, may as well hard code them from the beginning instead of creating them on the fly.

        The fact is that there are few good uses of symbolic references. An exception is the exporting of global variables between packages... but even this should be handled with care and often via the well-proven Exporter module. Many languages don't even allow the use of soft references, and yet they still get along just fine without them.


Re: Variable scalar creation.
by davido (Cardinal) on Feb 13, 2005 at 05:56 UTC

    Forgetting about symbolic references ( variables dynamically named ) and forgetting the above snippet, could you please explain what exactly you're trying to accomplish? It almost looks like you're trying to associate a bunch of field names with a bunch of values, and maybe trying to create variables named after those fieldnames, though the code above isn't quite doing that. With a little more explanation and clarification someone's going to come along and help you find the proper way to accomplish what you really need, instead of what you're currently asking for.

    If you want to just create a bunch of variables named after the contents of @names, you would probably do it this way:

    no strict qw/refs/; @::{@names} = \( @values );

    But that's a surefire way to cause yourself all sorts of headaches that you really don't want to suffer through. You know, the global symbol table, that you're trying to muck around with, is just a glorified hash. If it's good enough for Perl, it's probably good enough for you too: use a hash.

    UpdatedUsed a symbol-table slice instead of original foreach loop, just for fun and reckless disregard for the safety of mankind.


Re: Variable scalar creation.
by tall_man (Parson) on Feb 13, 2005 at 06:04 UTC
    If you must use symbolic references, you should turn off strict refs in as small a section of code as possible.
    use strict; $db->FetchRow(); my @names = $db->FieldNames(); my @values = $db->Data(); no strict "refs"; ${$names[$_]} = $values[$_] for 0..$#names; use strict;

    By the way, while ($names[$arraycnt] ne '') is an infinite loop. Uninitialized entries in an array are undef, not ''.

      undef is converted to the empty string when comparing it to a string. Give it a try:

      print "ne" while(undef eq '');
      re: By the way, while ($names$arraycnt ne '') is an infinite loop. Uninitialized entries in an array are undef, not ''.

      just tried something after you said this, i.e. got the loop to count and print the number every time it loops and it only counts to the exact number of the fields in the db, so it must have read the undef to equal ''?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://430530]
Approved by davido
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-05-29 08:14 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.