in reply to How can I get the name of the Variable using its reference

There really isn't an easy and pure-Perl way to figure out what names are attached to a particular scalar, array, or hash.

If you find yourself needing to find variable names, you may be better off storing your data in hashes where at least you can easily get a list of keys.

But basically if you find yourself needing to rediscover variable names, you probably have a design flaw. Maybe you could explain a little more about the situation so we could help with designing a solution.


Dave

  • Comment on Re: How can I get the name of the Variable using its reference

Replies are listed 'Best First'.
Re: Re: How can I get the name of the Variable using its reference
by posix_guy (Novice) on Feb 17, 2004 at 08:20 UTC
    Well Dave, its like this ..
    I am trying to write a module which can give the
    functionality of a database created using arrays and hashes ..
    i.e. create a virtual in memory database using arrays and hashes which could act as tables.
    And then allow running simple SQL statements to process data in these arrays/hashes just like database tables.

    eg. 1
    If the user choses to create the database using one "two dimensional array" named sales,
    I want him to be able to process the data in array sales using a simple query like ..
    select 0,sum(1) from sales group by 0 order by 0
    where 0 n 1 are the columns of array sales.
    O/P would be an array.

    This avoids the need for writing nested for loops in the calling program ..

    eg. 2
    user creates a database using a one "two dimensional" salary array and one "hash" parameters..
    Then he could use a query like below to select all the rows
    from array salary which have the salary value above a specified min salary.
    select salary.* from salary,parameters where salary.1 > parameters.minsal order by salary.0

    Ofcourse these are hypothetical examples and one would be
    better off getting processed o/p from the database itself.

    But this module which I am writing is for cases where my datasource is not database,
    and I need to have the ease of processing my datastructures like database tables.
    Let me know if more details are required.

      If you just want the benefits of an "in memory" database, then why not use DBD::SQLite instead.

      And can you please fix the formatting on that post please. The "pre" tags make it look really nasty.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      There are some expensive techniques involving trawling the symbol table, then every pad in every format and code reference which might be able to locate all the names by which a certain variable is referenced. If that failed then it might also be possible to write some C code to trawl through perl's arenas.

      The thing is, this is going to take a bunch of runtime because each lookup has to check a few thousand locations (if you're lucky). You'll be better off thinking of a different solution. Perhaps like SQLite or DBD::CSV.