in reply to Problems returning values from a db and putting into a hash

I looked over what you have and you are doing some dangerous scoping with the code you have posted. I see where you are coming from and hopefully what I post will push you in the right direction.

1. use strict, please read up on this module it is really important since it can help you find many of your errors.
2. Use gobal variables with caution.
3. Pass your data as a reference to your sub routines and scope them inside of the subroutines.
You didn't post all of your ListDados sub, but I do you are calling it on what is scoped as a global. Here is how I would write the top part of your code:
# your original queries format odd so I adjusted it slightly # think this may also be the source of your results # being duplicated my $sth = $dbh->prepare_cached(qq!SELECT DISTINCT A.p_code, A.business, A.status, A.value, A.login, A.e +nc_date, B.name, B.state FROM business A, users B WHERE A.login = ? AND A.data_enc_proposta = ? ORDER BY A.login, A.data_enc_proposta!); } $dbh = SysUtils::Connect; # $login_value and $data_enc_value have to come # from somewhere. $sth->execute($login_value,$data_enc_value); while (my ($rs,$neg,$st,$vp,$gel,$dte,$gen,$fil) = $sth->fetch +row_array()) { my $hashref = { $fil => { $gel => { $dte => { cliente => "$rs", negocio => "$neg", status => "$st", valor => "$vp", nome => "$gen" } } } } ); ListaDados($hashref); }
Then inside of your ListDados I would have something like this:
sub ListDados { my ($dados) = @_; foreach (keys %{$dados}) { $vf = "R\$ "; $tf = "R\$ "; $dados->{$fil}{$gel}{$dte}{valor} =~ s/\.0000$/\,00/; $vf .= $dados->{$fil}{$gel}{$dte}{valor}; if ($cnt eq 0) { $message .= qq!<TR class="texto3"> <TD colspan="4" BGCOLOR="$color2"><span class ="texto2">$dados->{$fil} +{$gel}{$dte}{nome}</span></TD> </TR> <TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0"> <TR CLASS="texto2"> <TD>Client</TD> <TD>Business</TD> <TD>Status</TD> <TD>Value</TD> </TR> <TR> <TD COLSPAN="4"> <HR> </TD> </TR> <TR CLASS="texto3"> <TD BGCOLOR="$color2">$dados->{$fil}{$gel}{$dte}{cliente}</TD> <TD BGCOLOR="$color2">$dados->{$fil}{$gel}{$dte}{negocio}</TD> <TD BGCOLOR="$color2">$dados->{$fil}{$gel}{$dte}{status}</TD> <TD BGCOLOR="$color2">$vf</TD> </TR>!; $cnt++; ($color2,$color1) = ($color1,$color2); } else { $message .= qq!<TR CLASS="texto3"> <TD BGCOLOR="$color2">$dados->{$fil}{$gel}{$dte}{cliente}</TD> <TD BGCOLOR="$color2">$dados->{$fil}{$gel}{$dte}{negocio}</TD> <TD BGCOLOR="$color2">$dados->{$fil}{$gel}{$dte}{status}</TD> <TD BGCOLOR="$color2">$vf</TD> </TR>!; ($color2,$color1) = ($color1,$color2); } } }
This is just some of the things you can do to clean it up.

NOTE: not throughly proofread, please CB me on any errors and I correct them.