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

I have the following example code which prints out a column from 1 to 20:

#!/usr/local/bin/perl foreach (1..10) { open (COUNT, ">>count.txt") or die "Can't create count.txt: $!\n"; print COUNT "$_\n"; close COUNT; } foreach (11..20) { open (NUM, ">>count.txt") or die "Can't create count.txt: $!\n"; print NUM "$_\n"; close NUM; }

what code would i need to use in the second foreach loop so to get two coulmns (1..10) and (11..20)?
harry

Replies are listed 'Best First'.
Re: sorting numbers
by Abigail-II (Bishop) on Aug 05, 2003 at 09:09 UTC
    Something like:
    open my $num, ">> count.txt" or die "Open: $!"; foreach (1 .. 10) { printf $num "%2d %2d\n", $_, $_ + 10; } close $num or die "Close: $!";

    Abigail

Re: sorting numbers
by broquaint (Abbot) on Aug 05, 2003 at 09:21 UTC
    It will likely be much simpler if you just open and close once, and if you're really just going to write out columns of numbers then you could do something like this
    open(COUNT, ">>count.txt") or die("Can't create count.txt: $!\n"); print COUNT $_, ' ', $_ + 10, $/ for 1 .. 10; close(COUNT);
    Which produces a file of two columns with the first column being 1 .. 10 and the second column 11 .. 20. If however you want to arbitrarily print 2 columns of numbers then the zip function from the Language::Functional module will be of help.
    HTH

    _________
    broquaint