Michom wrote:
HI,

Hello,

I am new to perl and I have written a smal script to grab data from
one file, and put them into another file. The problem is new lines,
which are printing nice under a linux environment, but it is all
messed up if I open it with notepad. I am running Perl 5 under cygwin.
Heres the script:


#!/usr/bin/perl

The next two lines should be:

use warnings;
use strict;

They will help you find mistakes in your code.


open (READ, "opt.txt") || die "Can't find opt.txt\n";

$x=0;

$x is not used anywhere so you don't really need it.

while ($info = <READ>) {
chomp $info;
@data = split (/\t/, $info);
push @cells, [...@data];
$x++;
}

close READ || die "Couldn't close opt.txt";

If you are going to use the higher precedence || operator then don't forget to use parentheses.

close( READ ) || die "Couldn't close opt.txt";

open (WRITE, ">rel.txt") || die "Can't find rel.txt\n";

$y=0;

print WRITE "#DoNotEditThisLine: UndoCommandFile off_files model_n_m_z
endfile=/tmp/6222\n\n";

foreach (@cells){
print WRITE "cr balla=1,blablabla=$cells[$y][0],foobar=$cells[$y][0]_
$cells[$y][1]\n";
print WRITE "bleh=10175,foounbar=$cells[$y][1]\n\n";

You are iterating over the @cells array so you don't really need $y you can just dereference the current element directly:

print WRITE
    "cr balla=1,blablabla=$_->[0],foobar=$_->[0]_$_->[1]\n",
    "bleh=10175,foounbar=$_->[1]\n\n";

$y++;
}

close WRITE || die "Couldn't close rel.txt";
exit (0);


Now if I cat the output in cygwin, the newlines are working. If I open
it with notepad, one new line is working which is after print WRITE
"bleh=10175,foounbar=$cells[$y][1]\n\n";  (please note not both of
newlines are working).

Any ideas? I need those in that specific format

You don't need two different loops, you can operate on both files at the same time:

#!/usr/bin/perl
use warnings;
use strict;

my $read_file  = 'opt.txt';
my $write_file = 'rel.txt';

open READ,  '<', $read_file  or die "Can't open '$read_file' $!";
open WRITE, '>', $write_file or die "Can't open '$write_file' $!";


print WRITE "#DoNotEditThisLine: UndoCommandFile off_files model_n_m_zendfile=/tmp/6222\n\n";

while ( my $info = <READ> ) {
    chomp $info;
    my @data = split /\t/, $info;

    print WRITE
        "cr balla=1,blablabla=$data[0],foobar=$data[0]_$data[1]\n",
        "bleh=10175,foounbar=$data[1]\n\n";
}

close READ  or die "Couldn't close '$read_file' $!";
close WRITE or die "Couldn't close '$write_file' $!";

exit 0;

__END__



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

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