smackdab has asked for the wisdom of the Perl Monks concerning the following question:

Hola Monks,

I got the basics to work, but I have to know how to crunch it down ;)

The last 3 lines work just fine, but how do I get rid of line number 1 ???
my %cmds = (valid stuff) go_cmds(\%cmds); sub go_cmds { my $r_cmds = shift; # Grab anon array (valid array) 1) my $r_names = $r_cmds->{"names"}; 2) my @names = @$r_names; 3) my $names_qty = $#colnames + 1;
This works, but how to remove line 1???

thanks,
-john

Edit kudra, 2001-09-19 Added code tags

Replies are listed 'Best First'.
Re: \ref
by blakem (Monsignor) on Sep 19, 2001 at 08:59 UTC
    You should be able to combine the first two lines into a single line like so:
    my @names = @{$r_cmds->{names}};
    And, while I don't see exactly where the array '@colnames' is coming from, your third line can be rewritten as well:
    my $names_qty = @colnames;
    In scalar context, @arr will evaluate to the number of elements it contains. Therefore the following expression is always[*] true ($#arr+1 == @arr)

    Is that what you are looking for?

    * for sane values of $[.... but it would be a bad idea to go mucking around with that anyway.

    -Blake

Re: \ref
by geektron (Curate) on Sep 19, 2001 at 09:06 UTC
    you would only want to get rid of line one in favor or a real accessor method.

    so, you could do:

     my @names = @{ $r_cmds->{"names"} };

    or, you could write a proper accessor method for the 'names' attibute ( if you have access to the original code that creates the 'r_cmd' object i assume you're using. )

    untested code

    sub names { my $obj = shift; my @names = @{ $obj->{'names'} }; return @names; }

    then, you would just have lines one and two look like this:

    sub go_cmds { my $r_cmds = shift; my @names = $r_cmds->names; ## do more stuff }
Re: \ref
by smackdab (Pilgrim) on Sep 19, 2001 at 09:19 UTC
    Thanks, that was easy...seems I need to put the extra {}'s before the "@" or the "$#" Now I do vaguely remember reading something about not having two of those characters next to each other...

    thanks!!!!
      Right, I figured that's your problem but didn't get here first to answer <g>.

      They are not extra braces. Rather, the braces may be optionally omitted if the thing is a simple scalar name.

      So, you can say:

      $temp= $x->foo(); @list= @$temp;
      because $temp is a simple scalar name. The canonocal form would be @{$temp}. So, to get rid of the temp, say:
      @list= @{$x->foo()};