Hi PerlMonks,

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!


In reply to 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.