in reply to Re^3: Perl code for finding shortest path not working on large files (chomp)
in thread Perl code for finding shortest path not working on large files

Thanks athan. Another help I need is that all this output I want it dumped in a output file. But I cant use this :
perl program.pl >> output.txt
Because as you can see the program asks for user input, so I cant redirect the output. Can you help me on how to redirect the output of such a code which asks for user input on command line.

There's some code that needs to be included around the last printf which is actually printing on console out, but exactly how to do it is what I want. Thanks !!

  • Comment on Re^4: Perl code for finding shortest path not working on large files (chomp)
  • Download Code

Replies are listed 'Best First'.
Re^5: Perl code for finding shortest path not working on large files (chomp)
by QM (Parson) on Jul 28, 2014 at 12:40 UTC
    zing, the short answer is to print the user prompt to STDERR, and redirect STDOUT as you suggest above.

    However, there's a more convenient way, if you're up to it. In your OP, you have the command line:

    perl short_path.pl input_fie output_file

    but the input and output filenames are never used in the script. To make use of them, you would access @ARGV, either explicitly or implicitly, instead of asking for the input filename.

    To do so, you delete this:

    print "Enter file name:\n"; chomp(my $file = <STDIN>); open(DATA,$file) or die "failed opening file!!";

    Where you have:

    ins split /,\s*/ for <DATA>;

    replace it with this (based on Athenasius's edit):

    for (<>) { chomp; ins split /,\s*/; }

    The <> is Perl DWIMmery for the magic <ARGV> filehandle, which steps through all the values in @ARGV as filenames to read from, and does the open for you. (If you expect to feed the script a list of files, you may want to add some user-friendly error checking.)

    Then to execute the script:

    perl short_path.pl input_file > output_file

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re^5: Perl code for finding shortest path not working on large files (chomp)
by Athanasius (Archbishop) on Jul 28, 2014 at 13:50 UTC

    QM’s approach is probably the best way to go. But, for completeness: another option is to use tee from the command line. The tee command allows standard output to go to the terminal as usual, while also copying it to one or more files:

    perl program.pl | tee -a output.txt

    Notes:

    • The -a option appends to the file instead of overwriting it.
    • You will also need to add the line:
      $| = 1;
      or its equivalent near the top of the Perl script to force a flush after every print statement (otherwise, you won’t see the Enter file name: prompt).
    • tee is standard on Unix-based operating systems. If you’re on Windows, you can download it as part of the GnuWin CoreUtils package from SourceForge.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,