On 07/07/2014 10:26 AM, Розанда ЧУП wrote:
Without comments is more clear :)
======================================
#!/usr/bin/perl -w
use strict;
use 5.010;

open FILE, '<', "numbers.txt" or die "can not open file . $!\n";
open FILE1, '>>', "sumfile.txt" or die "can not open file . $!\n";
select FILE1; # set the defualt output stream

chomp (my @lines = <FILE>);
my (@ln, $first, $second);

foreach (@lines) { @ln = split /\h/, $_;
                     $first .= (shift @ln)." ";
                     $second .= (pop @ln)." ";
}

print "$first \n$second";

close FILE;
close FILE1;
========================================



This worked for me:

#!/usr/bin/perl

  use strict;
  use warnings;

  my ($infile, $outfile);
  open $infile, "<", "numbers.txt" or
    die "Error opening numbers.txt for input\n";
  open $outfile, ">", "sumfile.txt" or
    die "Error opening sumfile.txt for output\n";

  chomp (my @lines = <$infile>);

  my @vals;
  push @vals, [(split ' ', $_ )] foreach @lines;

  my $row_max = $#vals;    # The last row index
  my $col_max = $#{$vals[0]};   # The last column index

  foreach my $outcol (0 .. $col_max) {
    my $output_line = '';
    foreach my $outrow (0..$row_max) {
      $output_line .= "$vals[$outrow][$outcol] ";
    }
    print {$outfile} "$output_line\n";
  }


  close $infile;
  close $outfile;

Also, it is not dependent upon a 2 x 5 input... it works with any number of rows and columns, as long as all of the records have the same number of fields



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to