http://www.perlmonks.org?node_id=1006082

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

hi there, just consider below example to understand my question

a f b g c h d i e j

in this i need to print form a to e and then to return in line of and print f to j. But i don't know how to return back from used " \n " sequences here, i mean i need to return back to my original position after using various " \n" statements. Thanks.

Replies are listed 'Best First'.
Re: need help to print some lines
by toolic (Bishop) on Nov 28, 2012 at 18:10 UTC
    One way is to store the 2nd column in an array as you read the lines:
    use warnings; use strict; my @c2; while (<DATA>) { my @cols = split; push @c2, $cols[1]; print "$cols[0]\n"; } print "$_\n" for @c2; __DATA__ a f b g c h d i e j

      without taking anything away from toolic beautiful solution, one can also still write this:

      use warnings; use strict; my $array_organizer = [ [], [] ]; while (<DATA>) { push @{ $array_organizer->[0] }, [split]->[0]; push @{ $array_organizer->[1] }, [split]->[1]; } for my $value ( @{$array_organizer} ) { print map { $_ } @{$value}, $/; } __DATA__ a f b g c h d i e j

        Instead of repeating push function, why not:

        ... while (<DATA>) { for my $index ( 0 .. 1 ) { push @{ $array_organizer->[$index] }, [split]->[$index]; } } ...

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: need help to print some lines
by ww (Archbishop) on Nov 28, 2012 at 21:20 UTC

    Rethink your notion that you can "print form (sic) a to e and then to return in line of and print f to j". You can't undo the printing of a newline in the manner you describe, any more than you can put the toothpaste back in the tube (or the genie back in the bottle, if you prefer).

    Instead, consider determining the length of your array and dividing by two to derive the appropriate values in Lns 8 & 9 (which are hardcoded below and left as an exercise for the OP):

    #!/usr/bin/perl use 5.014; # enable features, strict and warnings my @arr = qw(a b c d e f g h i j); my $i=0; while ( $i <=4 ) { my $j = $i+5; say "$arr[$i] \t $arr[$j]"; $i++; }

    Hint: reading about the var $#, scalar(@arr) or searching for "array length" will be instructive.

    Afterthought: obviously, I've understood meaning of the SOPW differently than some other Monks. Since OP provides no code, and only (IMO) an ambiguous spec, it seems plausible that the OP is trying to print a simple, 10 element array with a single letter is each element as a set of lines with the first half of the elements in column 1 and the second half in column 2.

Re: need help to print some lines
by myelinviolin (Novice) on Nov 28, 2012 at 19:12 UTC
    Are you looking for something like this? You can always write a function to determine where the boundaries of the for statements should be.
    $A[0][0]="a"; $A[1][0]="b"; $A[2][0]="c"; $A[3][0]="d"; $A[4][0]="e"; $A[0][1]="f"; $A[1][1]="g"; $A[2][1]="h"; $A[3][1]="i"; $A[4][1]="j"; for($col=0; $col<=1; $col++){ for($row=0; $row<=4; $row++){ print "$A[$row][$col]."; } }