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

Hi,

The code below is part of a script which produces a CSV file. I need it to populate each field in the file, regardless of value. In other words, if a key's value is 0, I want to print "0"( that's a zero ). Here's how I populate the hash, but when I print the hash, it doesn't print the key/value pair, when the value = zero

%jobstats = ( total_jobs => 0, successful_jobs => 0, partially_successful_jobs => 0, failed_jobs => 0, jobs_started_in_window => 0, jobs_ended_in_window => 0, jobs_active_in_window => 0, retried_jobs => 0 ); %jobs = bpdbjobs($code); tie %jobstats, "Tie::IxHash"; $jobstats{total_jobs} = keys %jobs; foreach $jobid ( keys %jobs ) { my $job = $jobs{$jobid}; next if $job->{status} == 150; ++$jobstats{successful_jobs} if $job->{status} == 0; ++$jobstats{partially_successful_jobs} if $job->{status} == 1; ++$jobstats{failed_jobs} if $job->{status} > 1; ++$jobstats{jobs_started_in_window} if ( $job->{started} >= $START_SECONDS ) && ( $job->{started} <= $END_SECONDS ); ++$jobstats{jobs_ended_in_window} if ( $job->{ended} >= $START_SECONDS ) && ( $job->{ended} <= $END_SECONDS ); ++$jobstats{jobs_active_in_window} if ( ( $job->{started} + $job->{duration} ) >= $START_SECONDS ) && ( ( $job->{started} + $job->{duration} ) <= $END_SECONDS ); ++$jobstats{retried_jobs} if $job->{trycount} > 1; }

Replies are listed 'Best First'.
Re: Print hash value even if it's zero
by gjb (Vicar) on Nov 18, 2002 at 20:52 UTC

    You'll want to use printf so that you can specify a format string.

    printf STDOUT "%d|", ($val);
    will do the trick (note the brackets). The %d specifies that you want to print an integer, similarly %s is a string, %f a floating point value, etc.

    See perlfunc for more details.

    Hope this helps, -gjb-

      You can also try

      print 0 + $val;
Re: Print hash value even if it's zero
by dws (Chancellor) on Nov 18, 2002 at 20:24 UTC
    but when I print the hash, it doesn't print the key/value pair, when the value = zero

    Show us the code that prints the hash.

      OK, here you go!
      open OUT, ">>$outfile" or die "Can't open $outfile for writing: $!\n"; print_hash_cvs(%jobstats); close OUT; exit(0); sub print_hash_cvs { my (%hash) = @_; my ( $key, $val ); foreach $key ( keys %hash ) { $val = $hash{$key}; print OUT "$val|"; } print OUT "$yesterday|$today\n"; }
        I don't see what this code isn't printing 0 (zero), but I do note that print_hash_cvs is printing values in essentially random order. Hashes don't guarantee key order. Most of the time, if you set up a hash the way you do, you'll get back keys in the same order, but it's not a good idea to assume that. If some other script is going to assume field order, then you're much better off being explicit about which fields you're writing.

        Something like:

        my @fields = qw(total_jobs ... retried_jobs); ... foreach my $key ( @fields ) { my $val = $hash{$key}; print OUT "$val|"; }
        is a safer.

Re: Print hash value even if it's zero
by metlhed_ (Beadle) on Nov 18, 2002 at 20:35 UTC

    Maybee you should post the whole program, because it should print the "0". There might be an error somewhere else.