in reply to Re^3: function like GetOption
in thread function like GetOption

Thanks BrowserUk for the idea, can you please give me an example of an hash of arrays of hashes or some documentation? bory

Replies are listed 'Best First'.
Re^5: function like GetOption
by BrowserUk (Patriarch) on Jun 16, 2005 at 11:20 UTC

    Something like this would do the trick. For some good documentation on using perl's data structures see perlreftut and perldsc.

    #! perl -lw use strict; use Data::Dumper; BEGIN{ our %channels; my $channel; while( @ARGV ) { $_ = shift @ARGV; if( m[-channel] ) { $channel = shift @ARGV; push @{ $channels{ $channel } }, {}; next ; } if( m[-([a-z])] ) { $channels{ $channel }[-1]{ $1 } = shift @ARGV; next; } die "Invalid arg '$_'"; } } our %channels; print Dumper \%channels; __DATA__ P:\test>perl 456642.pl -channel 1 -a 1 -b 1 -c 1 -channel 2 -a 2 -c 2 +-channel 3 -b 3 -c 17 -channel 1 -a 2 -c 2 $VAR1 = { '1' => [ { 'c' => '1', 'a' => '1', 'b' => '1' }, { 'c' => '2', 'a' => '2' } ], '3' => [ { 'c' => '17', 'b' => '3' } ], '2' => [ { 'c' => '2', 'a' => '2' } ] };

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Thank you for your help BrowserUk,I have another question regarding your code. How can I print (for eg) the values for channel number 1 for 'c'.I tried smth like this (it worked):
      $channels{1}[-1]{c} $channels{1}[-2]{c}
      but what if I want to be something generalized, instead of -1 and -2 to use an iterator?

        You need to follow those references I gave you and play with a few simple examples to understand them. For now, to iterate the elements of the array

        print $channels{1}[ $_ ]{c} for 0 .. $#{ $channels{1} };

        To explain that. $channels{1} contains an array (reference). To find out how many elements there are in the array pointed at by an array reference you do:

        my $arraySize = scalar @{ $arrayRef }; ## In this case my $arraySize = scalar @{ $channels{1} };

        But what you need here is the index of the highest element of the array, rather than the number of elements. Ie.

        my $highestIndex = scalar @{ $arrayRef } - 1;
        .

        Perl recognises that this is a frequently used value and gives you a shortcut to obtaining it. For a normal array @a, the size of the array is scalar @a and the index of the last element is $#a.

        When using an array reference that becomes $arrayRef, scalar @{ $arrayRef } and $#{ $arrayRef } respectively.

        Putting that all together, you can iterate the values of the 'c' keys in the array of hashes referenced by $channels{i} like this

        my $arrayRef = $channels{1}; my $arraySize = scalar @{ $arrayRef }; my $highestIndex = $#{ $arrayRef }; ## == $arraySize - 1; for my $index ( 0 .. $highestIndex ) { print $channels{1}[ $index ]{c}, "\n"; }

        But perl recognises that this type of operation is a very frequently used activity. It therefore provides the programmer with ways of shortcutting many of the intermediate steps, and the temporary variables that they require, and you can do the same thing like this.

        $/ = "\n"; ## See also perl -l print $channels{1}[ $_ ]{c} for 0 .. $#{ $channels{1} };

        And so, with a little reading of the docs and a little practice, 6 lines becomes 1 line. Less code invariably means less errors and the intent of the code becomes clearer. Perl is full of such practical aids to clarity ... it's a shame so many people eshew them.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.