Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Perl For loops

by d2caetano (Initiate)
on Jul 03, 2007 at 19:51 UTC ( #624763=perlquestion: print w/ replies, xml ) Need Help??
d2caetano has asked for the wisdom of the Perl Monks concerning the following question:

I need to convert the following into forloops.

Thanks for help.

my @matrix=([qw(1 2 4 4)],[qw(5 2 0 8)],[qw(9 10 11 12)],); my @t = map{my $c=$_; my $z=1;[grep $z&=/[^0]/, map{$matrix[$_][$c]}0. +.$#matrix]}0..$#{$matrix[0]};

Comment on Perl For loops
Download Code
Re: Perl For loops
by cdarke (Prior) on Jul 03, 2007 at 20:28 UTC
    Why? What are you trying to achieve?
    It would help for readability if you used code tags.
    map returns a new list, whereas for allows modification of an existing one. You need to take the input list in your map and use that as the for list. You then need to push your result onto the output array.
    But if your code works, then why rewrite it to use for?
      map returns a new list, whereas for allows modification of an existing one
      It sounds like contracdicting although, perhaps, you didn't mean it. The map operator allows modification as well since $_ would be a reference to the corresponding elements of existing array.
      $ perl -wle 'my @ar = qw(one two three); print "@ar"; my @new = map { +$_ = uc } @ar; print "@new"; print "@ar"' one two three ONE TWO THREE ONE TWO THREE

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

        True. Apologies.
Re: Perl For loops
by toolic (Chancellor) on Jul 03, 2007 at 20:53 UTC
Re: Perl For loops
by Util (Priest) on Jul 03, 2007 at 21:08 UTC

    Untested, and probably *completely* wrong due to lack of <CODE>...</CODE> tags in the OP:

    my @matrix = ( [ qw( 1 2 4 4 ) ], [ qw( 5 2 0 8 ) ], [ qw( 9 10 11 12 ) ], ); my @t; foreach my $column_num ( 0 .. $#{ $matrix[0] } ) { my @slice = map { $_->[$column_num] } @matrix; my @r; foreach my $n ( @slice ) { last if $n == 0; push @r, $n; } push @t, [@r]; }

Re: Perl For loops
by ikegami (Pope) on Jul 03, 2007 at 21:14 UTC
    my @array = map { STMT* EXPR } LIST;

    can be written as

    my @array; for (LIST) { STMT* push @array, EXPR; }

    and

    my @array = grep EXPR, LIST;

    can be written as

    my @array; for (LIST) { if (EXPR) { push @array, $_; } }
Re: Perl For loops
by aufflick (Deacon) on Jul 04, 2007 at 06:41 UTC
    This code doesn't need to be so complicated - for instance why is there an and in there? If the values are always simple integers then the whole $z thing is just testing for equality with 0.

    Given that, the loop is just aborting once a 0 values is hit. We also know that the length of the outer array in the result is the count of the first inner array in the matrix. Thus:

    my @matrix=([qw(1 2 4 4)],[qw(5 2 0 8)],[qw(9 10 11 12)],); my @t; for my $i (0..$#{$matrix[0]}) { for my $j (0..$#matrix) { last if $matrix[$j][$i] == 0; $t[$i][$j] = $matrix[$j][$i]; } }
    The result of both your code and mine is the same:

    $VAR1 = [ [ 1, 5, 9 ], [ 2, 2, 10 ], [ 4 ], [ 4, 8, 12 ], ]
    Update: I must have missed Util's comment, which is virtually identical to mine :)

      Hi Monks,

      I don't know what is the purpose of getting it in a "for" loop. But if we are concerned only about output or $t, can't we do something like
      $t = [ @matrix ]
      so that the entire program would be
      my @matrix=([qw(1 2 4 4)],[qw(5 2 0 8)],[qw(9 10 11 12)],); my $t = [ @matrix ];
      Or even simpler like
      my $t = [ [qw(1 2 4 4)],[qw(5 2 0 8)],[qw(9 10 11 12)], ];
      cheers!

      -VC

      Update: Appologies for the carelessness & waste of time.

        well, because your way $t is:
        [ [ 1, 2, 4, 4 ], [ 5, 2, 0, 8 ], [ 9, 10, 11, 12 ], ]
        when it should be:
        [ [ 1, 5, 9 ], [ 2, 2, 10 ], [ 4 ], [ 4, 8, 12 ], ]
        kinda different...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://624763]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-10-22 02:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls