in reply to How to ask user for file name and save the output to a new text file

Hello mmazlan67 and welcome to the monastery and to the wonderful world of Perl!

> got stuck on thinking how to open summary.txt file to read and open inout.txt to write at the same time.

This is very simple: you open a filehandle in read mode and another in write mode. You use the diamond operator <> too much: it is handy but also magic, infact instead of print "Enter the name of the file: "; my $base_dir = <>; you better my $base_dir = <STDIN>; ( $userinput is a better name than $base_dir anyway..) so start out with plain things:

# never forget the following!!! use strict; use warnings; # use diagnostics; # this can be skipped but is useful when starting # grab user input.. # choose a file name for the out file.. ... open my $readhandle, '<', $filetoread or die "Unable to read [$filetor +ead]!"; open my $writehandle '>', $filetowrite or die ""Unable to write [$file +toread]!"; # not <> but a named lexical filehandle while (<$readhandle>){ if ($_ =~ /somethingtosearch/){ # print to the write file print $writehandle $_; } } # always explicitally close filehandle: Perl will normally does the ri +ght thing but it is safer to close them anyway (good habit) close $readhandle; close $writehandle; # take the file and attach to an email ...

This is the basic. After you have a working skeleton code you can add some more feature as more error control or a more elegant way to parse command line arguments aka Getopt::Simple or Getopt::Long

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: How to ask user for file name and save the output to a new text file
by mmazlan67 (Novice) on Jun 19, 2017 at 08:39 UTC

    Hi, i get to understand the code more now, however i am a bit confused with this part:

    # grab user input.. print "Enter the name of the file: "; my $userinput = <STDIN>; chomp($userinput);
    # choose a file name for the out file..

    How do i choose the file name for outfile? Is it like asking user for two file name? Sorry as i am just a beginner for this language

      hello again,

      this is up to you: you can ask again via STDIN as you have done for the first file or you can hardcode the name into the script or you can craft it using the program name or the first file name:

      # option 1 print "Enter the name for the output file:\n"; my $outfile = <STDIN>; chomp $outfile; #parens not needed # option 2 my $outfile = 'output.txt'; # option 3a my $outfile = $firstfilename.'.out'; # option 3b - using $0 aka the current program filename use File::Basename qw(basename); my $outfile = (basename $0).'.out';

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Hi again, i did tried combine but somehow this errors appear.Is it what i am doing is totally wrong?

        String found where operator expected at step1_inpout.pl line 21, near "$writehandle "+>"" (#1) (S syntax) The Perl lexer knows whether to expect a term or an ope +rator. If it sees what it knows to be a term when it was expecting to see + an operator, it gives you this warning. Usually it indicates that an operator or delimiter was omitted, such as a semicolon. (Missing operator before "+>"?) String found where operator expected at step1_inpout.pl line 23, near "$outputhandle ">"" (#1) (Missing operator before ">"?) syntax error at step1_inpout.pl line 21, near "$writehandle "+>"" syntax error at step1_inpout.pl line 23, near "$outputhandle ">"" Execution of step1_inpout.pl aborted due to compilation errors (#2) (F) Probably means you had a syntax error. Common reasons include +: A keyword is misspelled. A semicolon is missing. A comma is missing. An opening or closing parenthesis is missing. An opening or closing brace is missing. A closing quote is missing. Often there will be another error message associated with the synt +ax error giving more information. (Sometimes it helps to turn on -w. +) The error message itself often tells you where it was in the line +when it decided to give up. Sometimes the actual error is several toke +ns before this, because Perl is good at understanding random input. Occasionally the line number may be misleading, and once in a blue + moon the only way to figure out what's triggering the error is to call perl -c repeatedly, chopping away half the program each time to se +e if the error went away. Sort of the cybernetic version of S<20 questions>. Uncaught exception from user code: syntax error at step1_inpout.pl line 21, near "$writehandle "+ +>"" syntax error at step1_inpout.pl line 23, near "$outputhandle ">"" Execution of step1_inpout.pl aborted due to compilation errors. at step1_inpout.pl line 46

        This is my code

        #! /tools/perl/5.8.8/linux/bin/perl use strict; use warnings; use diagnostics; # grab user input.. print "Enter the name of the file to read: "; my $filetoread = <STDIN>; chomp($filetoread); print "Enter the name of the file to write: "; my $filetowrite = <STDIN>; chomp($filetowrite); print "Enter the name of the output file: "; my $fileoutput = <STDIN>; chomp($fileoutput); open my $readhandle, "<", $filetoread or die "Unable to read '$filetor +ead'"; open my $writehandle "+>", $filetowrite or die "Unable to write '$file +towrite'"; open my $outputhandle ">", $fileoutput or die "Unable to write '$fileo +utput'"; while (<$readhandle>) { if ($_ =~ /^\s\s(\S+)*delay\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+ +(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+) +/) { print $writehandle $_; } } while(<$writehandle>) { my ( $set ) = m/^\s+(\S+)/; #get the first word my ( $name,$value ) = m/-name (\S+) (\S+)/; #get the name and + value my ( $mode ) = m/mode == (\S+)\"/; #get the mode print "$mode $name $set $value\n"; } close $readhandle; close $writehandle;