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)


In reply to Creating Hashes from Switches by johnirl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.