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

Hey Monks
I'm trying to read in a group of switches from the command line and add them and the prceeding values to a hash. I can't use perls built in functions as I have to read in switches on the fly from an XML document.

The command line would look like this

perl multiple.pl -o outputfile -x xmlfile -d disk1 disk2 disk3 -m memo +ry1 memory2 memory3

I will alway have the -x and -o switches but the rest are decided by the xml file. So I need to store them in a hash until I've read the XML file and then know what to do with them.

The hash should look like this

my %switches = ( -x => ["xmlfile"], -o => ["outputfile"], -d => ["disk1", "disk2", "disk3"], -m => ["memory1", "memory2", "memory3"]);

Click below to see my code

#!/usr/bin/perl use strict; use Getopt::Std; my @XMLarray; my @OUTarray; my @LEFTOVERarray; my $currentArray; foreach my $file (@ARGV) { if ($file eq "-x"){ $currentArray = \@XMLarray; } elsif ($file eq "-o"){ $currentArray = \@OUTarray; } elsif ($file =~ /-/){ unless ($file eq "-x" || $file eq "-o"){ push @LEFTOVERarray, $file; $currentArray = \@LEFTOVERarray; } } else{ push @$currentArray, $file; } } if (@XMLarray > 1){ die "Only one xml file allowed"; } print "\nXMLarray :\n"; foreach (@XMLarray){ print "$_\n"; } print "\nOUTarray :\n"; foreach (@OUTarray){ print "$_\n"; } print "\nLEFTOVERarray :\n"; foreach (@LEFTOVERarray){ print "$_\n"; } print "\n"; my %switches = ( -x => @XMLarray, -o => @OUTarray); foreach (@LEFTOVERarray){ my $key; my @array; if ($_ =~ /-/){ print "FIRST IF : $_\n"; if ($key){ $switches{$key} = @array; undef $key; undef @array; } else{ $key = $_ } } else{ "SECOND IF : $_\n"; push @array, $_; } }#foreach (@LEFTOVERarray) print %switches; print "\n";

j o h n i r l .

 Sum day soon I'Il lern how 2 spelI (nad tYpe)

Replies are listed 'Best First'.
Re: Creating Hashes from Switches
by davorg (Chancellor) on Sep 19, 2002 at 09:24 UTC

    Getopt::Std (which you use but never use) gets you most of the way there.

    #!/usr/bin/perl -w use strict; use Getopt::Std; use Data::Dumper; my %switches; getopts('x:o:d:m:', \%switches); print Dumper \%switches;

    Running it gets you:

    $ ./test.pl -o outputfile -x xmlfiles -d disk1 -m memory1 $VAR1 = { 'm' => 'memory1', 'd' => 'disk1', 'x' => 'xmlfiles', 'o' => 'outputfile' };

    The problem is, of course, with your multi-valued options. If you could make one small change to the way you pass them into the program then this would be very simple.

    #!/usr/bin/perl -w use strict; use Getopt::Std; use Data::Dumper; my %switches; getopts('x:o:d:m:', \%switches); foreach (keys %switches) { $switches{$_} = [ split /\s+/, $switches{$_} ]; } print Dumper \%switches;

    The one problem is that you have to quote the multiple keys like this:

    $ ./test.pl -o outputfile -x xmlfiles -d 'disk1 disk2' -m 'memory1 mem +ory2' $VAR1 = { 'm' => [ 'memory1', 'memory2' ], 'd' => [ 'disk1', 'disk2' ], 'x' => [ 'xmlfiles' ], 'o' => [ 'outputfile' ] };
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      for the @ARGV: make a block, local the @ARGV array and build it from the XML file. should work.
      Thanks Dave,
      but how would this be implemented if I didn't know the switches until runtime?

      UPDATE
      Well now I feel stupid. It seems my code was working I just forgot to print the results. Duh!! Sorry

      j o h n i r l .

       Sum day soon I'Il lern how 2 spelI (nad tYpe)

        If you know that the first two arguments you'll get are the XML file and the output file then you can do something like this:

        #!/usr/bin/perl -w use strict; use Getopt::Std; use Data::Dumper; my %switches; %switches = splice(@ARGV, 0, 4); # parse the XML file and work out what other arguments # you're going to be passed. Use that info to build $fmt my $fmt = 'd:m:'; getopts($fmt, \%switches); foreach (keys %switches) { $switches{$_} = [ split /\s+/, $switches{$_} ]; } print Dumper \%switches;

        It's a bit fragile tho' as it assumes there will be spaces between the first two options and their arguments.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg