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

#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use feature 'say'; my@array = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z); my@array_a = @array[0..5]; my@array_b = @array[6..8]; my@array_c = @array[8..15]; my@array_d = @array[16..16]; my@array_e = @array[17..20]; my@array_f = @array[21..24]; my@array_g = @array[25..16]; print "first five string:",@array_a,"\n"; print @array_b,"\n"; print @array_c,"\n"; print @array_d,"\n"; print @array_e,"\n"; print @array_f,"\n"; print @array_g;
I have a above Perl script that Prints the results of the script on the terminal screen. Instead of having the information printed on the screen, I need it to save it to a text file.

Replies are listed 'Best First'.
Re: How to save the output result in file
by hdb (Monsignor) on Jan 17, 2014 at 06:33 UTC
      i tried like my
      $filename = "/home/Ram/Desktop/Perl_file.txt"; open(my@array_a = @array[0..5], '>', $filename) or die "Could not open + file '$filename' $!"; print @array_a ; close @array_a; print "done\n";
      but it showing error like

      first five string:abcdef ghi ijklmnop q rstu vwxy Can't use string ("6") as a symbol ref while "strict refs" in use at array.pl line 22 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref. Uncaught exception from user code: Can't use string ("6") as a symbol ref while "strict refs" in use at array.pl line 22. at array.pl line 22

      . Why it showing error? Please let me know

        You need to add a variable for the filehandle:

        my $filename = "/home/Ram/Desktop/Perl_file.txt"; open(my $filehandle, '>', $filename) or die "Could not open file $file +name\n"; print $filehandle @array_a ; close $filehandle; print "done\n";
Re: How to save the output result in file
by AnomalousMonk (Archbishop) on Jan 17, 2014 at 09:04 UTC
    my@array_g = @array[25..16];

    Please be aware if you are not already that  @array[25..16] produces an empty list and not, as you might have been expecting, a reversed slice of the original array. See the "Range Operators" section in perlop.

Re: How to save the output result in file
by nithins (Sexton) on Jan 17, 2014 at 06:43 UTC

    Try This

    #!/usr/bin/perl -w use strict; use warnings; use diagnostics; use feature 'say'; my@array = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z); my@array_a = @array[0..5]; my@array_b = @array[6..8]; my@array_c = @array[8..15]; my@array_d = @array[16..16]; my@array_e = @array[17..20]; my@array_f = @array[21..24]; my@array_g = @array[25..16]; open(FH,'>','file.txt') or die "$!"; print FH "first five string:",@array_a,"\n"; print FH @array_b,"\n"; print FH @array_c,"\n"; print FH @array_d,"\n"; print FH @array_e,"\n"; print FH @array_f,"\n"; print FH @array_g;
Re: How to save the output result in file
by Anonymous Monk on Jan 17, 2014 at 08:30 UTC
    redirects the output into a file eg.
    shell> perl scriptname.pl > output.txt
Re: How to save the output result in file
by rammohan (Acolyte) on Jan 17, 2014 at 07:08 UTC
    its work fine. Is there any method to create and define a file and file name in Perl Script?

      Please re-read open.