#!/usr/bin/perl -w
use strict;
use 5.010;

open FILE, '<', "numbers.txt" or die "can not open file . $!\n"; # recommended 
to use open with three argument
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/, $_;  # divide $_ by all kind of horisontal 
whitespace (\h); @ln=(1,6)
                    $first .= (shift @ln)." "; # shift from @ln to $first and 
add the backspace; $first = "1 ";
                    $second .= (pop @ln)." "; # pop from @ln to $second and add 
the backspace; $second = "6 ";
}

print "$first \n$second";
      
close FILE;
close FILE1;

07.07.2014, 11:19, "Sunita Pradhan" <sunita.pradhan.2...@hotmail.com>:
> I have a file of contents:
> -----------
> 1 6
> 2 7
> 3 8
> 4 9
> 5 10
> ----------
> I want a file with content:
>
> 1 2 3 4 5
> 6 7 8 9 10
>
> --------
> I have written a few lines of following code but it does not work as expected 
>  :
>
> -------------------------------------------
> #!/usr/bin/perl
> use v5.10;
> use strict;
> use warnings;
>
> open(FILE, "numbers.txt") or die "can not open file . $!\n";
> open(FILE1, ">sumfile.txt") or die "can not open file . $!\n";
>
> my @lines = <FILE>;
> chomp @lines;
>
> my $c=0;
> while ($c <= $#lines){
>       if ($lines[$c] =~ /(\w+)\s+(\w+)/){
>            print FILE1 "$1\n";
>            print FILE1 "$2";
>
>       }
>
> $c++;
>        }
>
>        close FILE;
>        close FILE1;
> --------------------------
>
> Please guide me .

-- 
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