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

Hello, Can you give me some ideas of implementing a function (something like GetOption)which I want to act like follows for a command line:  dosomething -channel 1 -a 1 -b 1 -c 1 -channel 2 -a 2 -c 2 -channel 3 -b 3 -c 17 to store the values as follows: for channel no.1 we have a=1, b=1,c=1, for channel no.2 we have a=2, c=2, for channel no.3 we have b=3, c=17 etc. Thank you for your time. Best regards, bory

Replies are listed 'Best First'.
Re: function like GetOption
by Mutant (Priest) on May 13, 2005 at 09:36 UTC
    Command line parsing is a common problem so, not surprisingly, someone's already solved it for you.

    There's an entire hierarchy on CPAN dedicated to this, called Getopt. A couple of the more popular modules are: Browse the documentation, and see which one suits your needs.

    For any problem in Perl that might have a general solution, it's worthwhile having a look on CPAN. Chances are there's a module that does all the work for you. This is one of the best parts of Perl.
Re: function like GetOption
by BrowserUk (Patriarch) on May 13, 2005 at 09:55 UTC

    Something like this?

    #! perl -lw use strict; use Data::Dumper; BEGIN{ our %channels; my $channel; while( @ARGV ) { $_ = shift @ARGV; $channel = shift @ARGV and next if m[-channel]; $channels{ $channel }{ $1 } = shift @ARGV and next if m[-([a-z +])]; die "Invalid arg '$_'"; } } our %channels; print Dumper \%channels; __DATA__ P:\test>456642 -channel 1 -a 1 -b 1 -c 1 -channel 2 -a 2 -c 2 -channel + 3 -b 3 -c 17 $VAR1 = { '1' => { 'c' => '1', 'a' => '1', 'b' => '1' }, '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.
      Yes but I'm intrested in storing the values in some variables, exactly in this way proposed by you how can i call the value for 'c' from '2' for eg? Thanks again, bory
        how can i call the value for 'c' from '2' for eg?

        You are new to perl? The following will print the value of 'c' for channel 2:

        print $channels{2}{c};

        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.
      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

        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.