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

I am working on one script where I am planing to have my configuration file like this,
my %CFG = ( Command =>[DES=>"Copy", CMD=>"cp file1 file2'Hello'", DES=>"Remove" CMD=>"rm file1", DES=>"View" CMD=>"vi file1" ], );
to keep the right order I am using array inside hash. Now could any one suggest me how can I read this hash into my script and execute command one by one printing (DES) steps to the users??? Any suggestion please??

Replies are listed 'Best First'.
Re: Reading hash
by ikegami (Patriarch) on Jul 03, 2009 at 03:18 UTC

    to keep the right order I am using array inside hash

    No you're not. Your hash contains junk. Perhaps you were thinking of the following:
    %CFG = ( DES => [ "cp file1 file2", "rm file1", "vi file2", ], );
      I have updated my script, hope its better now. I want to be more logical so I am trying to use hash in my configuration file.
        If you want to be logical, you should consider pre-existing solution, like Apache Ant
Re: Reading hash
by jrsimmon (Hermit) on Jul 03, 2009 at 03:51 UTC
    First things first...that syntax is badly broken. Printing to Data::Dumper shows that your in-memory structure looks like this:
    $VAR1 = 'ARRAY(0x182ae14)'; $VAR2 = undef; $VAR3 = 'ARRAY(0x1b83664)'; $VAR4 = [ 'DES', 'Step2', 'CMD', 'rm file1' ];
    So, it sounds like you want to iterate over a hash and issue the commands contained therein. If the keys are known to you, and it appears that they are, there is no need to worry about order. You can simply step through each known key one at a time. What you want is something more like:
    my %better_cfg = (); $better_cfg{'DES'}{'Step1'} = "cp file1 file2"; $better_cfg{'DES'}{'Step2'} = "rm file1"; $better_cfg{'DES'}{'Step3'} = "vi file2";
    Which results in a data structure that looks like:
    $VAR1 = 'DES'; $VAR2 = { 'Step2' => 'rm file1', 'Step1' => 'cp file1 file2', 'Step3' => 'vi file2' };

    Now what you have is a hash embeded inside a hash (HoH). The outer hash, %better_cfg, has only one key -- DES. To step through the keys of the inner hash (which should be known to you so that you can maintain order), you'll use the syntax:
    my $step1 = $better_cfg{'DES'}{'step1'}; my $step2 = $better_cfg{'DES'}{'step2'}; my $step3 = $better_cfg{'DES'}{'step3'};
    If in fact you do not know the value of what will be the inner hash keys (step1, step2, step3), then you don't need to be using a hash at all. Simply read the values from the file into a hash in order, and then iterate over them in that order.
Re: Reading hash
by Marshall (Canon) on Jul 03, 2009 at 03:58 UTC
    I would suggest looking at any one of a number of config.ini Perl modules. I have used: use Config::INI::Reader; in some projects. There is a "Tiny" version.
    if you have something like:
    [sectionA] parm_1 = a, b, c [sectionB] something = x y z
    I would suggest using one of these modules.
Re: Reading hash
by bichonfrise74 (Vicar) on Jul 03, 2009 at 03:03 UTC
    I'm not sure why you would do this. Why not have a simple configuration file like below?

    DES
    cp file1 file2
    rm file1
    vi file2

    Then simply read and parse this line by line and execute it. Again, not sure why you would want to do this.
Re: Reading hash
by mzedeler (Pilgrim) on Jul 03, 2009 at 18:44 UTC

    Reading your example and considering that you want to keep the line order makes me conclude that you're not working on a configuration file, but a script.

    Is it so that the commands in the list will be executed in the order, you specify?

    If you can answer yes, you're reinventing scripts. Consider using bash or perl. If you need to customize the script you're writing somehow and get stuck doing that, post a question here.