Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Writing to text file

by annem_jyothsna (Initiate)
on Dec 14, 2010 at 13:53 UTC ( [id://877074]=perlquestion: print w/replies, xml ) Need Help??

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

In a loop i tried with
foreach my $x ( @finaldata ) { my ($sdrposttax, $reccurposttax); $sdrposttax = &FmtCur3($x->{'sdrposttax'}); $reccurposttax = &FmtCur3($x->{'reccurpotx'}); print FH "$x->{'period'},$x->{'partner'},$x->{'ftsqrg'},$x->{' +ltsqrg'},"; print FH "\"$sdrposttax\","; print FH "$x->{'reccur'},$x->{'recsdrrate'},"; print FH "\"$reccurposttax\","; # Compute the cumulative sum to display in the footer $sum += $x->{'sdrposttax'}; }
But in the text file i see like the below Oct-08,AAMNT,1,24,"","","0.000",EUR,1.0731,"","","0.000", where i don't know why the "" are coming in my report Plz help me in this

Replies are listed 'Best First'.
Re: Writing to text file
by roboticus (Chancellor) on Dec 14, 2010 at 13:59 UTC

    annem_jyothsna:

    The "" occur because you put them there: print FH "\"$sdrposttax\","; Inside double-quotes, \" means to place a quote inside the string. So "" simply means that $sdrposttax is an empty string (or undef), so you get adjacent quotes.

    Also, you should wrap your code and/or data in code tags <c>your code goes here</c> to make it easier to read. Otherwise, many people will simply ignore your post, as it can be annoying to have to decipher your post in order to give you help. Remember: When you're asking for help, it's in your own best interest to make it easy and convenient for us to help you. Offer us more information rather than less. Format it nicely.

    ...roboticus

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

Re: Writing to text file
by sundialsvc4 (Abbot) on Dec 14, 2010 at 14:26 UTC

    The “immediate-IF” syntax can come in handy here:   ( expression ? value_if_true : value_if_false )

    Another handy way to do it would be to define a little sub such as:

    sub quoted { my $value = shift; if (defined($value)) { return "\"$value\""; } else { return ""; } }

    Now you can print calls to this function.   It will return a quoted string if there’s anything inside, or an empty-string if not.   This kind of “shorthand” if applicable, can make code much easier to read.

      Code that compiles? When did this era end? For the benefit of posterity I must point out it's naïve and broken; handling quoting properly is not really a trivial problem–

      print quoted('"quoted"'), $/;

      And immediate if is a very un-Perly name for the ternary operator.

Re: Writing to text file
by Utilitarian (Vicar) on Dec 14, 2010 at 14:37 UTC
    It looks as though your FmtCur3 sub returns undefined

    PS: echoing other commentors remarks about formatting, it make a difference and leaving them code tags out causes headaches ;)

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found