johnirl has asked for the wisdom of the Perl Monks concerning the following question:
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 | |
by kabel (Chaplain) on Sep 19, 2002 at 09:37 UTC | |
by johnirl (Monk) on Sep 19, 2002 at 11:22 UTC | |
by davorg (Chancellor) on Sep 19, 2002 at 11:33 UTC |