Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Is it possible to write the results of a subroutine for data formatting to a text file?

by supriyoch_2008 (Monk)
on Aug 13, 2012 at 22:03 UTC ( [id://987225]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perl Monks,

I am a beginner in perl. I am interested in writing the results of a subroutine for data formatting to a text file. The cmd screen shows correct results but copying the results from cmd screen using the mouse is often difficult. So I want the results directly to a text file on desktop. Is there any perl syntax for this? I have given the perl code (fm.pl), the correct results of cmd and the incorrect text file Fmd.txt. below. How can I overcome this problem? Please suggest me some reading materials for writing to a text file in perl.

#!/usr/bin/perl-w $a="BATCATEATFATRAT"; $b=print_sequence($a,3); print"\n Scalar b=$b\n"; $output="Fmd .txt"; unless (open(RESULT,">$output")){ print"Cannot open file\"$output\".\n\n"; exit; } print RESULT"\n Formatted Data: $b\n\n"; close(RESULT); exit; sub print_sequence { my($sequence, $length) = @_; for (my $pos = 0; $pos < length($sequence); $pos += $length ) { print substr($sequence, $pos, $length), "\n"; } }

I have got the correct 3-letter words in cmd.

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\X>cd desktop C:\Users\X\Desktop>fm.pl BAT CAT EAT FAT RAT Scalar b= C:\Users\X\Desktop><code> <p> But the text file Fmd.txt does not show the words:</p> <code>Formatted Data:
  • Comment on Is it possible to write the results of a subroutine for data formatting to a text file?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Is it possible to write the results of a subroutine for data formatting to a text file?
by GrandFather (Saint) on Aug 14, 2012 at 00:59 UTC

    There are a bunch of things that can be improved in your script. The solution to your immediate problem is to have the sub return the result string instead of printing it. But not too:

    1. Don't use $a and $b as general purpose variables - they are special (used by sort). In fact always use sensible names, they help understanding a script much more than comments or additional documentation.
    2. Always use strictures (use strict; use warnings;). Note that use warnings; is better than including the -w flag on the command line.
    3. Use sensible indentation!
    4. Use three parameter open and lexical file handles.
    5. Avoid the C style for loop. Use a Perl style for loop instead. Not though that I've used a while loop and changed the way substr works instead.
    #!/usr/bin/perl use strict; use warnings; my $output = "Fmd.txt"; my $seq = "BATCATEATFATRAT"; my $result = splitSequence($seq, 3); print "\nResult:\n$result\n"; open my $outFile, '>', $output or die qq{Cannot open file "$output": $ +!.\n}; print $outFile "Formatted Data:\n$result\n\n"; close $outFile; sub splitSequence { my ($sequence, $length) = @_; my $result; while (length $sequence) { $result .= substr($sequence, 0, $length, '') . "\n"; } return $result; }

    Prints:

    Result: BAT CAT EAT FAT RAT
    True laziness is hard work
Re: Is it possible to write the results of a subroutine for data formatting to a text file?
by tobyink (Canon) on Aug 14, 2012 at 08:46 UTC

    GrandFather's answer is good.

    One thing I'll add though, is that there are cases where the subroutine you're calling and want to capture the output of is outside your control, and cannot be changed. In that case, Capture::Tiny is handy.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Is it possible to write the results of a subroutine for data formatting to a text file?
by aitap (Curate) on Aug 14, 2012 at 09:30 UTC
    Here are the comments to your program:
    • You don't use strict. You really should, as long as your program is more than several lines long. use warnings is also a bit better than perl -w because warnings pragma is local-scoped, but -w applies globally, including modules you use (but you are usually not supposed to fix warnings in them).
    • $b=print_sequence($a,3);
      sub print_sequence { my($sequence, $length) = @_; for (my $pos = 0; $pos < length($sequence); $pos += $length ) { print substr($sequence, $pos, $length), "\n"; } }
      You assign the $b variable the value of the expression print_sequence($a,3), but there is no return in your sub, so $b gets never assigned. You should return something from your sub if you want to assign it to a variable.
    • print"Cannot open file\"$output\".\n\n"; exit;
      This can be done easier using die: die qq{Cannot open file "$output"\n\n};. You have some problems with your quoting. Perhaps reading Quote and Quote like Operators will help you.
    • You don't indent your program. Use Perl::Tidy, set up your editor to help you indenting the program or switch to another editor or IDE.
    Sorry if my advice was wrong.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-25 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found