Check names of variables

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Check names of variables

    Hello everybody,

    I've written a perl-module that stores some settings in a DBM-hash.
    Currently, the module checks if certain keys have defined values in the
    hash and if so, it copies the values belonging to those keys to the
    corresponding variables.
    These variables are filled with default values before, so they work even
    if no such value is given in the hash.

    What I would like to do now: Iterate over the keys in the DBM-hash and
    look if a local variable of the same name exists; if so, copy the value
    from the hash to the corresponding local variable.

    Is such a thing possible? And if so, how should I do it? Could I create
    a command dynamically and run it via eval?

    Kind regards,

    Benjamin
    Benjamin Walkenhorst Guest

  2. Similar Questions and Discussions

    1. #39150 [NEW]: PHP allows invalid names for constants and variables
      From: z_rules55 at hotmail dot com Operating system: WinXP PHP version: 5.1.6 PHP Bug Type: Variables related Bug...
    2. #39150 [Opn->Bgs]: PHP allows invalid names for constants and variables
      ID: 39150 Updated by: tony2001@php.net Reported By: z_rules55 at hotmail dot com -Status: Open +Status: ...
    3. session names and variables
      Hi, I am trying to attach a variable to a session name Is it possible to accomplish something to the effect of the following? <cfloop...
    4. form button names as variables
      perhaps I am just a little tired, but I am having trouble with my form buttons. Firstly I name them like this name=\"round".$counter."heats\"...
    5. Help with $_POST variables. Reading their names.
      I have a dynamic form that that creates input variables named different things based on what it reads from the database. So, for example, it may...
  3. #2

    Default Re: Check names of variables


    Benjamin Walkenhorst wrote:
    >Hello everybody,
    >
    >I've written a perl-module that stores some settings in a DBM-hash.
    >Currently, the module checks if certain keys have defined values in the
    >hash and if so, it copies the values belonging to those keys to the
    >corresponding variables.
    >These variables are filled with default values before, so they work even
    >if no such value is given in the hash.
    >
    >What I would like to do now: Iterate over the keys in the DBM-hash and
    >look if a local variable of the same name exists; if so, copy the value
    >from the hash to the corresponding local variable.
    >
    My initial idea is this (assuming you're talking about identity between local variable name and hash key name):

    for (sort keys %dbm_hash)
    if (defined $$_) {
    $$_ = $dbm_hash{$_};
    }

    Since there is no hard reference in $_, the dereferencing should be treatedas a symbolic reference.

    - Jan
    --
    There are 10 kinds of people: those who understand binary, and those who don't
    Jan Eden Guest

  4. #3

    Default Re: Check names of variables

    >
    > Benjamin Walkenhorst wrote:
    >
    > >Hello everybody,
    > >
    > >I've written a perl-module that stores some settings in a DBM-hash.
    > >Currently, the module checks if certain keys have defined values in the
    > >hash and if so, it copies the values belonging to those keys to the
    > >corresponding variables.
    > >These variables are filled with default values before, so they work even
    > >if no such value is given in the hash.
    > >
    > >What I would like to do now: Iterate over the keys in the DBM-hash and
    > >look if a local variable of the same name exists; if so, copy the value
    > >from the hash to the corresponding local variable.
    > >
    > My initial idea is this (assuming you're talking about identity
    between local variable name and hash key name):
    >
    > for (sort keys %dbm_hash)
    > if (defined $$_) {
    > $$_ = $dbm_hash{$_};
    > }
    >
    > Since there is no hard reference in $_, the dereferencing should be
    treated as a symbolic reference.
    >
    But lets not go there first. What is with all of the copying in and out
    of hashes? What is the point? Generally you shouldn't need to do this
    type of thing, it usually indicates a problem elsewhere. A hash is
    usually a more useful data structure so pulling single values out and
    munging a bunch of scalar variables *usually* isn't the way you want to
    go about it.... so what are you really doing? Why don't you want to
    work with the values while they are in the %dbm_hash?

    [url]http://danconia.org[/url]


    Wiggins D Anconia Guest

  5. #4

    Default Re: Check names of variables

    Hello,

    On Wed, 11 Feb 2004 07:20:45 -0700
    "Wiggins d Anconia" <wiggins@danconia.org> wrote:
    > But lets not go there first. What is with all of the copying in and
    > out of hashes? What is the point? Generally you shouldn't need to do
    > this type of thing, it usually indicates a problem elsewhere. A hash
    > is usually a more useful data structure so pulling single values out
    > and munging a bunch of scalar variables *usually* isn't the way you
    > want to go about it.... so what are you really doing? Why don't you
    > want to work with the values while they are in the %dbm_hash?
    I know, I am going to switch over into that direction. I justed wanted
    to make sure the program works at all, while being able to test if the
    DBM is read correctly...
    Thus I had local variable filled with default values (so they wouldn't
    overwrite the values in the DBM-hash) and then override them with the
    values in the DBM-hash, if there were any. This way, if there was no
    value given in the DBM-hash for a certain setting, the program would not
    end up with invalid or empty values.

    Since it works now, I can get rid of the local variables.
    But still, I need two hashes, one for the default values, one for the
    overrides.
    The program allows you to change settings at run-time as well as change
    values in the dbm-hash - I don't want local-settings to go into the
    dbm-hash unless I explicitly choose to.

    Thanks,

    kind regards,

    Benjamin

    Benjamin Walkenhorst Guest

  6. #5

    Default Re: Check names of variables

    Benjamin Walkenhorst wrote:
    >
    > Hello everybody,
    Hello,
    > I've written a perl-module that stores some settings in a DBM-hash.
    > Currently, the module checks if certain keys have defined values in the
    > hash and if so, it copies the values belonging to those keys to the
    > corresponding variables.
    > These variables are filled with default values before, so they work even
    > if no such value is given in the hash.
    >
    > What I would like to do now: Iterate over the keys in the DBM-hash and
    > look if a local variable of the same name exists; if so, copy the value
    > from the hash to the corresponding local variable.
    >
    > Is such a thing possible? And if so, how should I do it? Could I create
    > a command dynamically and run it via eval?

    Instead of using "local variables" store your defaults in a hash, for
    example:

    my %defaults = (
    a => 1,
    b => 2,
    c => 3,
    d => 4,
    );

    use GDBM_File;
    tie %hash, 'GDBM_File', $filename, &GDBM_WRCREAT, 0640;

    @defaults{ keys %hash } = values %hash;

    untie %hash;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139