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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on how to move to the first row of array after passing it through a while loop
  • Download Code

Replies are listed 'Best First'.
Re: how to move to the first row of array after passing it through a while loop
by Athanasius (Archbishop) on Sep 01, 2015 at 04:35 UTC

    Hello wangruonan,

    Your code has the following skeletal form:

    my @headers; for $a (@list) { open(my $data, ...); while (my $line = <$data>) { while (my $fields = $csv->getline( $data )) { push @headers, $fields->[$a]; } } print LOG $headers[2], "\t"; close $data; }

    The obvious problem here is that once @headers contains at least 3 elements, the output will always be the same, viz., the third element!

    But it’s also important to note that reading from the filehandle $data directly — my $line = <$data>and via getlinemy $fields = $csv->getline( $data ) — is a logic error. You shouldn’t need the outer while loop; re-write the inner one to traverse the whole file.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: not able to pass foreach value to a while loop
by 1nickt (Canon) on Sep 01, 2015 at 03:48 UTC

    Update: The OP was completely changed while I wrote this reply, including the subject. OP below. My answer to the revised question further below.

    Original post:

    #!/usr/local/bin/perl use strict; use warnings; my $i=1; my @array = (34,543,754,5432,865,623,8635,5324,62,8654); my @list = (1, 3, 5, 8); foreach $a(@list){ print "foreach print value of a: $a\n"; while ($i) { print "while print value of a: $a\n"; $i=$i-1; print $array [$a], "\n"; } }
    for some reason, i want to pass certain value in @array to the while loop, but my code only allows me to pass the first value. Below is the result i want to get, Can anyone help me out? T_T
    foreach print value of a: 1 while print value of a: 1 543 foreach print value of a: 3 while print value of a: 3 5432 foreach print value of a: 5 while print value of a: 5 623 foreach print value of a: 8 while print value of a: 8 62
    Original post ends here


    Reply starts here

    Use for:

    my $value = $array[$_] for @list;
    #!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; my @array = (34,543,754,5432,865,623,8635,5324,62,8654); my @list = (1, 3, 5, 8); say "\$a: $_ \$array[$_] $array[$_]" for @list; __END__
    Output:
    $ perl 1140597.pl $a: 1 $array[1] 543 $a: 3 $array[3] 5432 $a: 5 $array[5] 623 $a: 8 $array[8] 62 $
    The way forward always starts with a minimal test.
Re: how to move to the first row of array after passing it through a while loop
by 1nickt (Canon) on Sep 01, 2015 at 05:33 UTC

    Use Text::CSV_XS::fragment():

    my @rows = $csv->fragment( *DATA, "col=11;15;18" );
    #!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Text::CSV_XS; my $csv = Text::CSV_XS->new( { binary => 1, auto_diag => 1 } ); my $wanted = join ';', ( 11, 15, 18 ); foreach my $row ( @{ $csv->fragment( *DATA, "col=$wanted" ) } ) { say "@{ $row }"; } __DATA__ A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i,h,g,f,e,d,c,b,a
    Output:
    $ perl 1140597-2.pl K O R p l i $
    The way forward always starts with a minimal test.
Re: how to move to the first row of array after passing it through a while loop ( mark updates)
by Anonymous Monk on Sep 01, 2015 at 03:51 UTC