in reply to Re: Dereferenced Arrays
in thread Dereferenced Arrays

i have successfully completed my goal of shifting out the first element in each array, gettin the length of the element, and eventually printing out the calculated amount of them all put together.

this is by far the UGLIEST function i have seen. the flow control is wretched.... but its a beautiful fault in its own "crack in the liberty bell" sorta way

poo ( [1..4], ["goo", "doo", "moo"], [6..9] ); sub poo { my($first, $second, $third) = @_; my @array = @{$second}; my $second = shift(@array); my $length = length($second); print("@array" , " $second", " $length\n"); @array = @{$first}; my $first = shift(@array); my $length1 = length($first); print("@array", " $first", " $length1\n"); @array = @{$third}; my $third = shift(@array); my $length2 = length($third); print("@array", " $third", " $length2\n"); my $result = $length + $length1 + $length2; print("$result\n"); }

no indents..... no comments... completely unmaintainable... but complete. only thing i have to ask now, is what suggestion would someone make to incorporate a loop to go thru. would it call for @_ [ $index ] and then increasing it throughout the loop until it returns undef?


thanks for all your guys' help, i really appreciate it.

Replies are listed 'Best First'.
Re^3: Dereferenced Arrays
by Marshall (Canon) on Aug 19, 2009 at 02:01 UTC
    The first issue here is what is "poo". Let's assign "poo" to a variable before we try to call a sub named "poo_sub".
    #!/usr/bin/perl -w use strict; use Data::Dumper; my @poo = ( [1..4], ["goo", "doo", "moo"], [6..9] ); print Dumper (\@poo); #here the Dumper module is used (it is VERY handy) to #show the structure of @poo. __END__ $VAR1 = [ [ 1, 2, 3, 4 ], [ 'goo', 'doo', 'moo' ], [ 6, 7, 8, 9 ] ];
    The [....] are anonymous array references. A list is well, a list of things. In this case @poo is a list of anon ref's to other lists. There is some distinction between list and an array, but in general this is just Perl internals and for the purpose of understanding what a "@var" is, makes no difference.

    So let's call a subroutine with the list of 3 things in @poo:

    #!/usr/bin/perl -w use strict; use Data::Dumper; my @poo = ( [1..4], ["goo", "doo", "moo"], [6..9] ); #first we just have 3 things, just pass 'em! 3 things in the call! poo (@poo); sub poo { my ($lref1, $lref2, $lref3) = (my @lrefs) = @_; #yes can do this, here we don't need individual ref's #just shown to demo that you can do that... foreach (@lrefs) { dump_lref ($_); } } sub dump_lref { my $lref = shift; #same as $lref = @_; here print "@$lref\n"; } __END__ prints: 1 2 3 4 goo doo moo 6 7 8 9
    Now let's say that we don't want to pass the 3 things in the @poo list, we just want to pass a reference to the @poo list:
    #!/usr/bin/perl -w use strict; use Data::Dumper; my @poo = ( [1..4], ["goo", "doo", "moo"], [6..9] ); #now just pass a ref to the @poo list ..... poo (\@poo); sub poo { my $ref_of_ref_to_list = shift; foreach my $lref (@$ref_of_ref_to_list) #@does one level of de-ref { dump_lref($lref); } } sub dump_lref ###WHOA same sub we used before! { my $lref = shift; #same as $lref = @_; here print "@$lref\n"; } __END__ prints: 1 2 3 4 goo doo moo 6 7 8 9
    WHOA! same printout!

    I think that I should mention that unlike some other languages, @list, $list, %list, sub list{} are all different things.

      sub dump_lref ###WHOA same sub we used before! {

      I've never seen anyone so excited by code reuse before! :)

        ;-) Just was trying to show that deference one level of an additional level of indirection is the same thing as before. I am glad that somebody got at least some humor value out my attempt! :-)

        Update: Kidding aside, I am genuinely trying to help. If the OP still has questions, I would be glad to try to help.

Re^3: Dereferenced Arrays
by ig (Vicar) on Aug 19, 2009 at 07:13 UTC

    You might like PerlTidy. It doesn't do comments, but it does do a good job of normalizing your code layout. It has reasonable defaults which you can override if you prefer some other style. It formats your code as:

    poo( [ 1 .. 4 ], [ "goo", "doo", "moo" ], [ 6 .. 9 ] ); sub poo { my ( $first, $second, $third ) = @_; my @array = @{$second}; my $second = shift(@array); my $length = length($second); print( "@array", " $second", " $length\n" ); @array = @{$first}; my $first = shift(@array); my $length1 = length($first); print( "@array", " $first", " $length1\n" ); @array = @{$third}; my $third = shift(@array); my $length2 = length($third); print( "@array", " $third", " $length2\n" ); my $result = $length + $length1 + $length2; print("$result\n"); }

    Not much different, but very easy to do.

    I would use strict and warnings. If you did, you would see warnings like the following: