Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

need help to print some lines

by vkp (Novice)
on Nov 28, 2012 at 18:00 UTC ( #1006082=perlquestion: print w/ replies, xml ) Need Help??
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.

Comment on need help to print some lines
Download Code
Re: need help to print some lines
by toolic (Chancellor) 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 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]."; } }
Re: need help to print some lines
by ww (Bishop) 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-09-17 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (95 votes), past polls