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

Hi Monks!

I am having an issue here that I can't think how to solve it, the problem is that the result(s) back from this code is not return the sum I am expecting, I found by running the commented test code on code that the value for $type "W" is been returned with an extra space after the W, like: $VAR1 = [ [ 'W ', '4' ] ]; as you can see here.
How could I remove any extra spaces before they reach the line
my $array_ref = $sth->fetchall_arrayref(); or any other solution to prevent that.

Thanks for the help!!!
.... my $sth = $dbh->prepare($sql); $sth->execute() || die $sth->errstr; #test values here::: #use Data::Dumper; #my $array_ref = $sth->fetchall_arrayref; #print Dumper $array_ref; #end test::: my $array_ref = $sth->fetchall_arrayref(); foreach $row (@$array_ref) { ($type, $total) = @$row; if ($type eq "A"){$a = $total;} if ($type eq "B"){$b = $total;} if ($type eq "C"){$c = $total;} if ($type eq "D"){$d = $total;} if ($type eq "W"){$w = $total;} } $total = $a + $b + $c + $d + $w; print "$w\n"; print "$total\n";

Replies are listed 'Best First'.
Re: Extra Space Issue
by Fletch (Bishop) on Jan 31, 2008 at 15:12 UTC

    The values are coming back from your database that way, so either don't store them with the space to begin with or run the column in question through something like a TRIM(column) function in your select statement.

    Having said that, that use of multiple ifs and 5 different variables looks bletcherous. Use a hash instead keyed on $type and it'd be much cleaner.

    my %totals; # ... my( $type, $total ) = @{ $row }; $totals{ $type } += $total; #... my $grand_total += $totals{ $_ } for qw( A B C D W ); print "W: $totals{ 'W' }\n"; print "grand total: $grand_total\n";

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Extra Space Issue
by pilcrow (Sexton) on Jan 31, 2008 at 16:29 UTC
    How could I remove any extra spaces before they reach the line my $array_ref = $sth->fetchall_arrayref(); or any other solution to prevent that.

    If it is true that you cannot modify the fetchloop processing -- pity, because that code wants to be refactored and would be an easy place to handle data normalization -- then you might normalize in the DB engine itself with $sql.

    Try the ANSI SQL SUBSTRING() or DBMS-specific TRIM/RTRIM(). Or, as Fletch suggested, clean up your data before you store it.

    -Mike

    Updated: More, better grammar. :)

Re: Extra Space Issue
by toolic (Bishop) on Jan 31, 2008 at 15:11 UTC
    I'm not sure how to prevent the extra space from being returned, but this is one way to remove it after the fact:
    ... ($type, $total) = @$row; $type =~ s/\s+$//; # Remove trailing whitespace if ($type eq "A"){$a = $total;} ...
Re: Extra Space Issue
by KurtSchwind (Chaplain) on Jan 31, 2008 at 15:11 UTC

    I'm not sure I have enough to work with here. A dump of @row would have been nice. Well, regardless, here are some observations that may help.

    • Pre-init your variables. $a = 0; $b = 0; . . . before going into your foreach.
    • Use regex instead of eq to test for 'W'. /W\s*/ if your datatype can be more than 1 char.
    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
      Pre-init your variables. $a = 0; $b = 0;

      Better not to use $a and $b at all. Save them for sort comparators.

      • another intruder with the mooring in the heart of the Perl

Re: Extra Space Issue
by Anonymous Monk on Jan 31, 2008 at 15:20 UTC
    one approach that avoids package variables (and the magical package variables $a and $b) might be (UNTESTED):

    my $grandtotal = 0; foreach my $row (@$array_ref) { my ($type, $total) = @$row; if ($type =~ m{\A \s* (?: A|B|C|D|W) \s* \z}xms) { $grandtotal += $total; } }