in reply to How to modify a file

Of course. Perl can do anything :)

# Add this somewhere before the loop # open output file my $outfile = "some_filename.ext"; open my $out, '>', $outfile or die "can't open $outfile: $!\n";

then change say $text; to say $out $text;. Look out!, there is no comma, between the output file handle and the data you want to send to it.

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: How to modify a file
by static0verdrive (Initiate) on Apr 25, 2013 at 13:58 UTC
    One small note... with only one '>' in the open line, the file will be overwritten each time this script runs. If you want to create a file to write to *or append to it if it exists already* be sure to use 2 '>>' instead.

    Here's how I do it:
    my $logfile = "/var/log/my.log" my $text = "Log entry text!" open(LOG, ">>$logfile") or die "Could not open file: $!"; print LOG $text; close LOG;
Re^2: How to modify a file
by biancoari (Initiate) on Apr 25, 2013 at 17:05 UTC

    Hi, I made this modification:

    #!/usr/local/bin/perl open my $FILE, '<', 'rapipago.txt' or die "Cannot open file1.txt: $!"; my $outfile = "rapipagomod.txt"; open my $out, '>', $outfile or die "can't open $outfile: $!\n"; use strict; use warnings; use feature 'say'; while (my $text = <$FILE>) { chomp $text; my @cums = (0); my @offs = ( 17, 6, 31, 3, 3 ); my @cums = ( 0 ); push @cums, $_ + $cums[ -1 ] for @offs; substr $text, $_, 0, q{-} for reverse @cums[ 1 .. $#cums ]; say $outfile $text; }

    And I am receiving this error:
    "Can't use string ("rapipagomod.txt")as a symbol ref while "strict refs" in use at C:\Users\ARIBIA~1\Desktop\Rapipago.pl line 15, <$FILE> line1."

      $outfile is the filename. You need to use the file handle $out.

      say $out $text;

      poj