I have added some additional print statements to your program. These will show you, step by step, how the offending elements are added to your data structure.

use Data::Dumper; my $cmd = "dfm host add [ -N ] [ -U <hostLogin> -P <hostPassword> ] { +all | <objects> ... | -H <hosting-storage-system> <vfiler> ... | - +T <trip> | -S <saf> } <end>"; print "\nCMD : $cmd\n"; my $arg_hash = getCmdParams($cmd); print Dumper ($arg_hash); sub getCmdParams { my $cmd = shift; my @temp = split(/\s+/,$cmd); my (@param,@switch,@arg,@alt_par); my ($line,$alt_cnt); my @mandate = (); my @optional = (); my $alt_line = ""; my %switch_param; my $switch_optional; foreach $line(@temp) { # handle alternate options if ($line =~ /^\{/){ $alt_cnt = 1;next; } $alt_cnt = 0 if ($line =~ /\}$/); #print "\nLINE : $line\nALT_CNT : $alt_cnt \n"; if ($alt_cnt == 1){ $alt_line.= $line; next ; } if ($alt_line ne ""){ print "\nLINE : $line ALT LINE: $alt_line\n"; @alt_par = split(/\|/,$alt_line); print "\nALT PAR LIST\n"; foreach (@alt_par){ print "\nOPTION : $_\n"; if (/.*?(\-[a-zA-Z]){1}.*?\<(.*?)\>/i){ print "HERE: \"$1\", \"$2\"\n"; $switch_optional->{"$1"} = $2; print "\$switch_optional = " . Dumper( +$switch_optional); @optional = (@optional,$switch_optiona +l); # ----HERE print "\@optional = " . Dumper(\@optio +nal); $switch_param{"optional"} = [@optional +]; print "\$switch_param{\"optional\"} = +" . Dumper($switch_param{optional}); }elsif(/.*?([a-zA-Z]+).*/i){ print "else HERE: \"$1\"\n"; @optional = (@optional,($1)); print "\@optional = " . Dumper(\@optio +nal); $switch_param{"optional"} = @optional; print "\$switch_param{\"optional\"} = +" . Dumper($switch_param{optional}); }else{ } $alt_line =""; next; } } # handles assigning switches to their respective parameters , handles +single switches also, handles mandatory parameteres without switches if ($line =~ /^\-[a-zA-Z]{1}/){ #print "\n$line\n";getc(); push (@switch,$line); } if (($line =~ /\<(.*?)\>/g)){ push (@arg,$1); $switch_param{shift @switch} = "" if ((scalar @switch) + > 1); $switch_param{shift @switch} = pop @arg unless ((scala +r @switch) == 0); } $switch_param{shift @switch} = "" if ((scalar @switch) > 1); $switch_param{"mandatory"} = [(@mandate,@arg)] if ((scalar @ar +g) > 0); } return \%switch_param; }

If you run this modified version of your program, you may be able to see which commands are adding the "additional" elements. You should be able to figure out which statement is adding the elements if you follow it one step at a time.

An alternative to adding print statements is to run your program in the debugger. Perl has a nice, built-in debugger. Some people prefer using the debugger and some people prefer using print statements. Either way, you can see how your statements are changing your data.

Solving the problem is a little tricky, given your data structure. What you need to do is append $switch_optional to the @optional array only once, instead of once each time you find a flag with an argument.

There is an error when 'all' and 'objects' are added to $switch_param{"optional"}. Can you see it? It is corrected when the hash reference is added. (hint: hash values are scalars. They can be references to arrays, but not whole arrays. What do you get when you evaluate an array in scalar context?).


In reply to Re: Hash ref assignment to array results in anomaly by ig
in thread Hash ref assignment to array results in anomaly by perlpal

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.