| Author |
how summarise results from 2 hashes?
|
|
| Maxipoint Rep Office 2004-10-28, 3:57 pm |
|
how summarise results from 2 hashes?
it will not summarise key(s) from hasfromform2! I only have hasfromform1
foreach(keys %{$hasfromform1}){
if($forma->{$_} eq $_){
$hasfromform1 += $hasfromform2->{$_};
}
| |
| Charles K. Clarkson 2004-10-28, 3:57 pm |
| Maxipoint Rep Office <maxipoint@st.htnet.hr> wrote:
: how summarise results from 2 hashes?
What do you mean by "summarise"?
: it will not summarise key(s) from hasfromform2! I only have
: hasfromform1
:
: foreach(keys %{$hasfromform1}){
: if($forma->{$_} eq $_){
: $hasfromform1 += $hasfromform2->{$_};
You are attempting to increment a hash reference. That
doesn't make sense. Did you mean to use this?
$hasfromform1->{$_} += $hasfromform2->{$_};
: }
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
| |
| Maxipoint Rep Office 2004-10-28, 3:57 pm |
|
: how summarise results from 2 hashes?
What do you mean by "summarise"?
RE: I mean: every key of $hasfromform1 + every key of $hasfromform2
: it will not summarise key(s) from hasfromform2! I only have
: hasfromform1
:
: foreach(keys %{$hasfromform1}){
: if($forma->{$_} eq $_){
: $hasfromform1 += $hasfromform2->{$_};
You are attempting to increment a hash reference. That doesn't make
sense. Did you mean to use this?
$hasfromform1->{$_} += $hasfromform2->{$_};
: }
RE: what I must to do?
-GP
| |
| Maxipoint Rep Office 2004-10-28, 3:57 pm |
|
Additional: $hasfromform1 have as result only 1 key
$hasfromform2 can have multiple keys (ever key by $hasfromform2 must be +
with other $hasfromform2 key)
and at the end go: one key from $hasfromform1 + every key of $hasfromform2
: how summarise results from 2 hashes?
What do you mean by "summarise"?
RE: I mean: every key of $hasfromform1 + every key of $hasfromform2
: it will not summarise key(s) from hasfromform2! I only have
: hasfromform1
:
: foreach(keys %{$hasfromform1}){
: if($forma->{$_} eq $_){
: $hasfromform1 += $hasfromform2->{$_};
You are attempting to increment a hash reference. That doesn't make
sense. Did you mean to use this?
$hasfromform1->{$_} += $hasfromform2->{$_};
: }
RE: what I must to do?
-GP
| |
| Charles K. Clarkson 2004-10-28, 3:57 pm |
| Maxipoint Rep Office <maxipoint@st.htnet.hr> top-posted:
: Additional: $hasfromform1 have as result only 1 key
: $hasfromform2 can have multiple keys (ever key by
: $hasfromform2 must be +
: with other $hasfromform2 key)
A hash contains keys and values. Are you trying to
sum all the keys or all the values of $hasfromform2?
Show us what is in each of the hashes you are using.
$hasfromform1
$forma
$hasfromform2
: and at the end go: one key from $hasfromform1 + every
: key of $hasfromform2
To get the sum of every value and every key in a
hash, use this.
use List::Util 'sum';
my %hash;
@hash{ 1 .. 4 } = 5 .. 9;
printf "Keys: %s\n", sum keys %hash;
printf "Values: %s\n", sum values %hash;
: : how summarise results from 2 hashes?
:
: What do you mean by "summarise"?
:
: RE: I mean: every key of $hasfromform1 + every key of
: $hasfromform2
You said above that there is only 1 key in $hasfromform1.
use Data::Dumper 'Dumper';
my %hash1 = (
1 => 2,
);
my %hash2 = (
1 => 2,
3 => 4,
);
my %sums;
foreach ( keys %hash1 ) {
$sums{ $_ } = $hash1{ $_ } + $hash2{ $_ };
}
print Dumper \%sums;
__END__
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
| |
| Maxipoint Rep Office 2004-10-28, 3:57 pm |
| in fact foreach(keys %{$hasfromform1}){
: if($forma->{$_} eq $_){
: $hasfromform1 += $hasfromform2->{$_};
work when $hasfromform2 has 1 key only. But if I select more keys
calculation is not ok!
my $total = $hasfromform1 + $hasfromform2;
| |
| Maxipoint Rep Office 2004-10-28, 8:55 pm |
|
-----Original Message-----
From: Charles K. Clarkson [mailto:cclarkson@htcomp.net]
Sent: Thursday, October 28, 2004 8:37 PM
To: beginners@perl.org
Subject: RE: how summarise results from 2 hashes?
Maxipoint Rep Office <maxipoint@st.htnet.hr> top-posted:
: Additional: $hasfromform1 have as result only 1 key
: $hasfromform2 can have multiple keys (ever key by
: $hasfromform2 must be +
: with other $hasfromform2 key)
A hash contains keys and values. Are you trying to sum all the keys or
all the values of $hasfromform2?
Show us what is in each of the hashes you are using.
$hasfromform1
$forma
$hasfromform2
RE: inside are numbers!
my $hasfromform1 = $hasfromform1->{$forma->{hasfromform1valuefromform}};
my $hasfromform2 = $hasfromform2->{$forma->{hasfromform2valuefromform}};
above send all numbers from form into my .pm
: and at the end go: one key from $hasfromform1 + every
: key of $hasfromform2
To get the sum of every value and every key in a hash, use this.
use List::Util 'sum';
my %hash;
@hash{ 1 .. 4 } = 5 .. 9;
printf "Keys: %s\n", sum keys %hash;
printf "Values: %s\n", sum values %hash;
RE:
in fact foreach(keys %{$hasfromform1}){
: if($forma->{$_} eq $_){
: $hasfromform1 += $hasfromform2->{$_};
work when $hasfromform2 has 1 key only.
How List::Util include into it?
-GP
| |
| Charles K. Clarkson 2004-10-29, 8:55 pm |
| Maxipoint Rep Office <maxipoint@st.htnet.hr> wrote:
: Charles K. Clarkson [mailto:cclarkson@htcomp.net] wrote:
:
: : Maxipoint Rep Office <maxipoint@st.htnet.hr> top-posted:
: :
: : : Additional: $hasfromform1 have as result only 1 key
: : : $hasfromform2 can have multiple keys (ever key by
: : : $hasfromform2 must be +
: : : with other $hasfromform2 key)
: :
: : A hash contains keys and values. Are you trying to sum
: : all the keys or
: : all the values of $hasfromform2?
: :
: : Show us what is in each of the hashes you are using.
: :
: : $hasfromform1
: : $forma
: : $hasfromform2
:
: RE: inside are numbers!
: my $hasfromform1 =
: $hasfromform1->{$forma->{hasfromform1valuefromform}};
:
: my $hasfromform2 =
: $hasfromform2->{$forma->{hasfromform2valuefromform}};
Thanks, but that doesn't tell me what is in there. Show us
the actual values in all three hashes. You can get the values
using the Dumper() function from the Data::Dumper module.
use Data::Dumper 'Dumper';
print Dumper( $hasfromform1, $forma, $hasfromform2 );
: above send all numbers from form into my .pm
I have no idea what that means.
: : : and at the end go: one key from $hasfromform1 + every
: : : key of $hasfromform2
: :
: : To get the sum of every value and every key in a hash, use
: : this.
: :
: : use List::Util 'sum';
: :
: : my %hash;
: :
: : @hash{ 1 .. 4 } = 5 .. 9;
: :
: : printf "Keys: %s\n", sum keys %hash;
: : printf "Values: %s\n", sum values %hash;
: :
: RE:
:
: in fact foreach(keys %{$hasfromform1}){
: : if($forma->{$_} eq $_){
: : $hasfromform1 += $hasfromform2->{$_};
:
: work when $hasfromform2 has 1 key only.
Show us a working example. Something we can run from our
own computer.
: How List::Util include into it?
I don't understand your question.
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
|
|
|
|