Saravanakumar Chinnaraj wrote:
> I'm having some problem in perl scripting. here is
> the scenerio. I have to read data from a file, which is in the format
> 
> head1
> sub1
> sub2
> sub3
> 
> head2
> sub4
> sub5
> 
> head3
> sub6...
> 
> so on. "head" is around 10 and "sub" varies from 2 to 10. My o/p has to be
> something
> like below.
> 
> head1      head2     head3
> sub1       sub4      sub6
> sub2       sub5      sub7
> sub3
> 
> I don't know how to go to previous line to print it. Do we have something
> similar to "\n", if so please let me know.

It's a cool problem. 
I don't know, whether there's a module solving this problem, but here's
my solution:

First treat whole the data as a single line (As you described, it won't
be the problem) -> my $data.

Then split at single lines, that's:
my @parts = split (/\n\n/, $text);   # or \r\n if you're using a
Mickysoft Windows

# Then split parts in rows ->
my $max_row = 0;
my @columns;
foreach (@parts) {
  # Split head1 -> sub1 sub2 sub3 ... at whitespaces
  my @column = split (/\s/, $_); 
  $max_row = @column if @column > $max_row;  
  push @columns, [ @column ];
}

# Let's print all rows
# Between the columns a put a tab, of course there can be anything you
need
for (my $row; $i<$max_row; $row++) {
        print $_->[$row], "\t" foreach (@columns);
        print "\n";     
}

Hope, that can solve the problem.
Again, I hope, that anybody can show us a 4 liner,
there should be one.

Best Wishes,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

  • Report saravanakumar chinnaraj
    • Andrea Holstein

Reply via email to