process multiple hashes

Discussion in 'Perl Misc' started by Joe, Jun 2, 2014.

  1. Joe

    Joe Guest

    This may be naive, but here is a situation:

    I have a number of hashes stored in a config file, each contains different set of data and they all have the same data structure and will be processedin the same way. I want to build a loop to process them one at a time. Nowmy question is, is there a way for me to get all there hash names (as hashreference I guess), put them in an array so I can build a 'foreach' to process them? (I have all hash names share something in common, like %AAlevel,%BBlevel, %CClevel, etc., and naively wished to grab them by REGEX :)

    I did some search and didn't seem to find anything close, and hope to have a luck here. Thanks in advance,

    joe
     
    Joe, Jun 2, 2014
    #1
    1. Advertisements

  2. Joe

    Tim McDaniel Guest

    I think it would be good to see a sample of the existing config file,
    and any sample code notions to use it. There are different
    possibilities, and by seeing existing code, it might be possible to
    suggest something closer to the existing code.
     
    Tim McDaniel, Jun 3, 2014
    #2
    1. Advertisements

  3. Joe

    Jim Gibson Guest

    You need to increase the level of indirection and indexing by one.

    In other words, create a %bighash that contains all of the other
    hashes, indexed by the name of the hash:

    %bighash = ( AAlevel => \%AAlevel, BBlevel => \%BBlevel, ... );

    Then, you can iterate over the keys of big hash, apply your regex to
    the names to select them, and process (or not) the individual hashes.
     
    Jim Gibson, Jun 3, 2014
    #3
  4. Joe

    Justin C Guest

    You say the file contains hashes, are they perhaps YAML or XML, or
    some other easily parsed data encoding type? If they are then there's
    probably a module for that. If they're not, why aren't they?!!!

    Justin.
     
    Justin C, Jun 3, 2014
    #4
  5. If the hashes are 'globlal' variables, you could loop through all keys
    of the symbol table of the package they exist in (this symbol table is
    accessible as a hash %package_name::, eg %main:: for package main) and
    look for 'well known names', cf (will not compile with strict)

    ---------------
    %a_hash = qw(a b c d);
    %b_hash = qw(e f g h);
    %c_hash = qw(i j k m);

    my $cur;
    for (keys(%main::)) {
    /_hash$/ or next;

    print("found $_\n");
    $cur = \%$_;
    print("\t$_ => $cur->{$_}\n") for keys(%$cur);
    }
    ---------------

    NB: This uses a symbolic reference (\%$_) to access the actual
    hash. That can be (at least) replaced with

    *{$::{$_}}{HASH}

    and strict won't complain about that (%main:: can also be accessed as
    %::).

    Alternate idea: Reorganize your config file such that the hashes are
    part of some 'top-level data structure', eg (untested)

    %hashes = (
    a_hash => {a => 'b', c => 'd'},
    b_hash => {e => 'f', g => 'h'});

    This avoids the need to do anything fancy to access the actual hash
    given its name and you won't accidentally pick up 'unrelated things with
    similar names' which also happen to exist in the symbol table.
     
    Rainer Weikusat, Jun 3, 2014
    #5
  6. Because perl can parse Perl and Perl supports the necessary syntactical
    constructs for describing complex data structures, 'config files' can be
    written in Perl and then parsed with perl via do '/etc/something'.

    Writing a Perl module in order to parse Perl so that people could
    download it instead of using the features built into the perl-program
    they already have would surely be the ultimate 'public CPAN masturbation'
    exercise. Assuming someone did this (I halfway fear that someone did
    really already do this, presumably, as broken XS version supposed to be
    fast, as 'pure Perl implementation' which works but is regarded to be
    slow and additionally as pure-Perl Perl::Tiny combining the advantages
    of both aproaches: It doesn't work and does so slowly), would people
    still recommend that?
     
    Rainer Weikusat, Jun 3, 2014
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.