perlpal has asked for the wisdom of the Perl Monks concerning the following question:
When i add a hash ref to an array , an additional element is added to the code. Looking for the reason for this and how i can avoid it.
The code is mentioned below , with --HERE indicating the code that adds the hash array element and the corresponding additional elements added.
#!/usr/bin/perl 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){ $switch_optional->{"$1"} = $2; @optional = (@optional,$switch_optiona +l); ----HERE $switch_param{"optional"} = [@optional +]; }elsif(/.*?([a-zA-Z]+).*/i){ @optional = (@optional,($1)); $switch_param{"optional"} = @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; }
The output :
$VAR1 = { '-P' => 'hostPassword', '-N' => '', 'mandatory' => [ 'end' ], 'optional' => [ 'all', 'objects', { '-T' => 'trip', '-H' => 'hosting-storage-system', '-S' => 'saf' }, $VAR1->{'optional'}[2], ----HERE $VAR1->{'optional'}[2] ----HERE ], '-U' => 'hostLogin' };
Thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash ref assignment to array results in anomaly
by Corion (Patriarch) on Jul 24, 2009 at 11:54 UTC | |
by perlpal (Scribe) on Jul 24, 2009 at 12:11 UTC | |
by Corion (Patriarch) on Jul 24, 2009 at 12:22 UTC | |
|
Re: Hash ref assignment to array results in anomaly
by GrandFather (Saint) on Jul 24, 2009 at 12:06 UTC | |
by perlpal (Scribe) on Jul 24, 2009 at 12:17 UTC | |
by GrandFather (Saint) on Jul 24, 2009 at 20:36 UTC | |
|
Re: Hash ref assignment to array results in anomaly
by ig (Vicar) on Jul 24, 2009 at 13:01 UTC | |
by perlpal (Scribe) on Jul 24, 2009 at 18:43 UTC | |
|
Re: Hash ref assignment to array results in anomaly
by jwkrahn (Abbot) on Jul 24, 2009 at 15:03 UTC |