in reply to Trimming of the decimal part.

You can remove the fractional part by using int( ). If you don't know in advance which array element is a number you can use looks_like_number( ) from the Scalar::Utils package:

Untested:

use strict; use warnings; use Scalar::Utils qw(looks_like_number); while (my $data = $_src_dbh->get_array()) { foreach my $elem (@$data) { $elem = int($elem) if looks_like_number($elem); } local $, = ','; print @$data; print "\n"; }
I assume now that $data is an array ref and @data was a typo in your code and should have been @$data.

Replies are listed 'Best First'.
Re^2: Trimming of the decimal part.
by mr_mischief (Monsignor) on Apr 23, 2008 at 18:17 UTC
    It should be noted that int always rounds down, while sprintf will flexibly round the proper direction. I didn't notice which way the OP wanted, nor whether the distinction would matter in the particular case. It's a good issue of which to be aware, though.
      I know that int( ) is truncating the fractional part away, i.e. is always rounding down. The OP said: But I do not want the decimal part printed in the above output.. This means truncation to me. Maybe I took it to literal.

      So far I can remember my last encounter with true rounding in Perl the sprinf function is really the best, like you already said. For other rounding functions like floor() and ceil() the POSIX module can be used.

      Update:
      Just tested sprintf("%d") and it truncates like int(). A function which implements "normal" rounding, away from zero from .5 starting is:

      sub round ($) { return sprintf("%.0f", shift); }