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

Hello, I come back with almost the same problem, using your code for this command line synthax:
456642 -channel 1 -a 1 -b 1 -c 1 -channel 1 -a 2 -c 2 it prints only $VAR1 = { '1' => { 'c' => '2', 'a' => '2' } };
but I want to print also the first values for channel number 1, like this:
$VAR1 = { '1' => { 'c' => '1', 'a' => '1', 'b' => '1' }, '1' => { 'c' => '2', 'a' => '2' } };
Thank you for your time, bory

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

    Hash keys are unique. You cannot have 2 identical keys in the same hash. You would have to use some other data structure to represent your data. Maybe an hash of arrays of hashes?

    $var = { 1 => [ { a=>, b => 1, c=> 1 }, { a => 2, c => 2 }. ] };

    That said, how can channel.1.a be both 1 & 2?

    I'm not party to your application details, but that really doesn't make a lot of sense to me.


    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.
      Thanks BrowserUk for the idea, can you please give me an example of an hash of arrays of hashes or some documentation? bory

        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.