Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How can one get the result to a text file when print join is used?

by supriyoch_2008 (Scribe)
on Feb 27, 2013 at 12:24 UTC ( #1020869=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I am interested to get the output of the following perl script (p.pl) to a text file. I have tried in several ways but I always get only the first two lines and not the actual permutations. FH does not work when I write it in between print and join. Usually FH is written between print and " and it nicely works. Here it does not. I am looking forward to perlmonks for suggestions.

Here goes the script p.pl:

#!/usr/bin/perl -l use warnings; use strict; use Algorithm::Permute; ########################################################### # Opening file to write the program's output. ################################################### open(FH, ">myPer .txt") or die "Cannot open myPer .txt"; ########################################################### my @arr = qw/A B C/; my $N=scalar(@arr); my $permu=2; my $p = new Algorithm::Permute([@arr], $permu); print"\n WELCOME to Permutation Analysis:\n All Possible Permutations of $permu Elements out of $N .i.e. $N P $pe +rmu:\n"; print FH "\n WELCOME to Permutation Analysis:\n All Possible Permutations of $permu Elements out of $N .i.e. $N P $pe +rmu:\n"; while (my(@res) = $p->next) { do {print join("", @res);};} while (my(@res) = $p->next) { do {print join("", @res);};} close FH; exit 0;

Here goes the incomplete output of text file myPer .txt:

WELCOME to Permutation Analysis: All Possible Permutations of 2 Elements out of 3 .i.e. 3 P2:

Comment on How can one get the result to a text file when print join is used?
Select or Download Code
Re: How can one get the result to a text file when print join is used?
by Corion (Pope) on Feb 27, 2013 at 12:26 UTC

    Perl comes with extensive documentation. See the documentation for the print function.

      Hi Corion,

      Thanks for your suggestions. I shall read the suggested text on print.

      Regards

Re: How can one get the result to a text file when print join is used?
by marto (Chancellor) on Feb 27, 2013 at 12:32 UTC
Re: How can one get the result to a text file when print join is used?
by roboticus (Canon) on Feb 27, 2013 at 12:34 UTC

    supriyoch_2008:

    If you want your output to go to the file, you need to say so. Change this:

    print join("", @res);

    to this:

    print FH join("", @res);

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Or add a "select FH;" after the "open (FH...")

      open(FH, ">myPer .txt") or die "Cannot open myPer .txt"; select FH;

        Hi clueless newbie,

        Thanks for the code. It has worked and my problem is now over. The code is unique and simple.

        With deep regards,

      Hi roboticus,

      Thanks for the code. It has worked nicely and solved my problem.

      Regards

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020869]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-10-22 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (119 votes), past polls