"Guruguhan N" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]

> Hi All,

Hello.

Please read Charles K. Clarkson's comments. They are spot on. I will not
repeat them here.

> In reply to my own posting, I have written a code like the one given
below.
>  @X = (1 .. 30)
>  $n_el = scalar(@X);

Scalar is unnecessary here. $n_el forces scalar context. Just use @X below
where you now have $n_el. One fewer variable is good.
:-)

>  $n_row = $n_el/3;      # 3 is the number of columns I want.

$n_row = @X / 3;

>  for ($i=0; $i<$n_row; $i++) {
>     for ( $j=$i; $j <$n_el; $j+=$n_row) {

                        @X

>     printf (  "%4d\t", $X[$j]);
>     }
>     printf ( "\n");

Use print instead. You are not formatting here. print() is more efficient.

The following code works for numeric and non-numeric data. This problem
gives the appearance of being very simple. However, appearances are often
deceptive.
;-)

The following codes works:

-------BEGIN CODE-------
#!/usr/bin/perl
use warnings;
use strict;

my @data;
my @X    = 1 .. 30;
my $cols = 3;
my $rows = int(@X / $cols) + (@X % $cols ? 1 : 0);

# unfortunately you have to add
# everything in @X to @data
for my $i( 0 .. $#X ) {
  push @{ $data[$i % $rows] }, $X[$i];
}

foreach (@data) {
  print join("\t", @$_) . "\n";
}
--------END CODE--------


Hope this helps,
ZO



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to