Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

printf function alternative? formatting without printing

by fraizerangus (Sexton)
on Jan 09, 2010 at 15:21 UTC ( [id://816487]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Everyone Sorry for the rookie question but, I'm using
$n -= ($n * $u) * $u; printf "%d\n", abs($n);
iterating through to get a result I want, but I want to save this result in an index of an array, but the above function just prints it to the screen I can't do anything with it?! to save the result and process it further is there anyway I can format it and save it as a string, in a for loop? e.g.
array [$i] = (printf "%d\n", abs($n));
many thanks and apologies if this question seems or sounds stupid to you?

Replies are listed 'Best First'.
Re: printf function alternative? formatting without printing
by SankoR (Prior) on Jan 09, 2010 at 15:34 UTC
Re: printf function alternative? formatting without printing
by FunkyMonk (Chancellor) on Jan 09, 2010 at 16:16 UTC
    Generally speaking, Perl treats numbers and stringified numbers as equal (ie perl -e "print 'equal' if '6.0' == 6" will print "equal"). So int will do just as good a job as sprintf.
Re: printf function alternative? formatting without printing
by ww (Archbishop) on Jan 09, 2010 at 18:41 UTC

    Both the advice from SankoR and, even more so, the observation by FunkyMonk are clearly on target. In the hope of offering some additional clarity:

    printf produces output; sprintf merely formats

    However, I'm perplexed by your use of "string" in the phrase "save it as a string" as well as by some other aspects of the question. For example, do you mean that $n and $u are floating point numbers (assumed below) or can they be ints; a mixture; or something else??

    All the above leads me to send a question back: Does this come somewhere close to your intent?

    #!/usr/bin/perl use strict; use warnings; # 816487 my $n = 3.123; my $u = 3.678; my $i= 0; my (@array, $array); for (1..10) { $n -= ($n * $u) * $u; $array[$i] = int(abs($n)); $i++ } for (@array) { print "$_ \n"; }

    Output, for $n=3.123, $u=3.678

    39 490 6140 76922 963664 12072486 151240294 1894690621 23736085384 297358177092

    and for $n=3.123, $u=4.0

    46 702 10540 158101 2371528 35572921 533593828 8003907421 120058611328 1800879169921

    And another question: "Waaaay off? Helpful?" (I hope the latter.)

Re: printf function alternative? formatting without printing
by toolic (Bishop) on Jan 09, 2010 at 18:43 UTC
Re: printf function alternative? formatting without printing
by ikegami (Patriarch) on Jan 09, 2010 at 19:04 UTC
    If you're dealing with integers,
    $array[$i] = abs($n);

    If you're dealing with floats,

    use POSIX qw( ceil floor ); $array[$i] = int(abs($n)); # Truncates $array[$i] = sprintf('%d', abs($n)); # Rounds $array[$i] = ceil(abs($n)); # Finds ceiling $array[$i] = floor(abs($n)); # Finds floor

    Add the "\n" when you print. There's no reason to store it in the array.

Re: printf function alternative? formatting without printing
by ravi.s (Initiate) on Jan 09, 2010 at 21:19 UTC
    You might have to save the output and "push" those values into array instead of printing the value. Sample Code : <code>for ( condition ) { $n -= ($n * $u) * $u; push(@i,"$n"); }<code>
Re: printf function alternative? formatting without printing
by mnooning (Beadle) on Jan 09, 2010 at 18:55 UTC
    Yes. sprintf FORMAT, LIST Just use sprintf instead of printf, and assign it to the array.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-03-28 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found