in reply to Hash ref assignment to array results in anomaly

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

Replies are listed 'Best First'.
Re^2: Hash ref assignment to array results in anomaly
by perlpal (Scribe) on Jul 24, 2009 at 18:43 UTC
    thank you for pointing out the error with respect to the array in scalar context.