FAQ
I guess I'm not understanding what I'm doing (or have been reading in my
web searches)... Can someone explain the difference between these two
code snippets...

SUN83-PRODWEB>more foo
#! /usr/local/bin/perl
use strict;
my %t;
my ($a, $c);
my ($aa, $cc);
$a=$aa=1;
$c=$cc=q{Additional Electrical Circuit};
$t{$a}{$c}{AMT}=1000;
$c=q{Additional};
$t{$a}{$c}{AMT}=2;
print $t{$aa}{$cc}{AMT}, "\n";
&Ix($t{$aa}{$cc}{AMT});
sub Ix {
my ($v) = @_;
print "v=$v\n";
}
SUN83-PRODWEB>./foo
1000
v=1000
SUN83-PRODWEB>


And
1 #! /usr/local/bin/perl -d
2 use strict;
106 my ($prev_fund, $prev_service, $prev_acct);
107 my %totals=();
108 while ($sth->fetch) {
109 if ($fund ne $prev_fund) {
110
&PrintAcctTotal($totals{$prev_fund}{$prev_service}{$prev_acct}{AMT},
111
$totals{$prev_fund}{$prev_service}{$prev_acct}{QTY});
112
&PrintServiceTotal($totals{$prev_fund}{$prev_service}{AMT},
113 $totals{$prev_fund}{$prev_service}{QTY});
114 &PrintFundTotal($totals{$prev_fund});
115 }
116 elsif ($service ne $prev_service) {
117 print
"(prev_fund,prev_service)=<$prev_fund,$prev_service>\n";
118 print "value
=<$totals{$prev_fund}{$prev_service}{AMT}>\n";
119
&PrintServiceTotal($totals{$prev_fund}{$prev_service}{AMT},
120 $totals{$prev_fund}{$prev_service}{QTY});
...
150 $totals{$fund}{$service}{$acct}{AMT} += $amt;
151 $totals{$fund}{$service}{$acct}{QTY} += $n_qty;
152 $totals{$fund}{$service}{AMT} += $amt;
153 $totals{$fund}{$service}{QTY} += $n_qty;
154 $totals{$fund} += $amt;
155 $prev_fund = $fund;
156 $prev_service = $service;
157 $prev_acct = $acct;
158 }

SUN83-PRODWEB>./journal_items.pl

Loading DB routines from perl5db.pl version 1.27
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(./journal_items.pl:17): my %param = (
main::(./journal_items.pl:18): PERIOD=>'',);
DB<1> c
(prev_fund,prev_service)=<Y,Additional Electrical Circuit>
Can't use string ("3862926") as a HASH ref while "strict refs" in use at
./journal_items.pl line 118.
at ./journal_items.pl line 118
Debugged program terminated. Use q to quit or R to restart,
use O inhibit_exit to avoid stopping after program termination,
h q, h R or h O to get additional info.
DB<1> q
SUN83-PRODWEB>

Thank you in advance...
John W Moon

Search Discussions

  • Mr. Shawn H. Corey at Jun 23, 2006 at 6:06 pm

    On Fri, 2006-23-06 at 13:36 -0400, Moon, John wrote:
    1 #! /usr/local/bin/perl -d
    2 use strict;
    106 my ($prev_fund, $prev_service, $prev_acct);
    107 my %totals=();
    108 while ($sth->fetch) {
    109 if ($fund ne $prev_fund) {
    110
    &PrintAcctTotal($totals{$prev_fund}{$prev_service}{$prev_acct}{AMT},
    111
    $totals{$prev_fund}{$prev_service}{$prev_acct}{QTY});
    112
    &PrintServiceTotal($totals{$prev_fund}{$prev_service}{AMT},
    113 $totals{$prev_fund}{$prev_service}{QTY});
    114 &PrintFundTotal($totals{$prev_fund});
    115 }
    116 elsif ($service ne $prev_service) {
    Examine %totals here. (You can also add the following to get %totals
    displayed on STDOUT:
    use Data::Dumper;
    print Dumper \%totals; # %totals will be called $VAR1
    )
    117 print
    "(prev_fund,prev_service)=<$prev_fund,$prev_service>\n";
    118 print "value
    =<$totals{$prev_fund}{$prev_service}{AMT}>\n";
    This is generating an error because you are going (at least) one level
    too deep. In other words, %totals doesn't have the structure you think
    it has.


    --
    __END__

    Just my 0.00000002 million dollars worth,
    --- Shawn

    "For the things we have to learn before we can do them, we learn by doing them."
    Aristotle

    * Perl tutorials at http://perlmonks.org/?node=Tutorials
    * A searchable perldoc is at http://perldoc.perl.org/
  • Tom Phoenix at Jun 23, 2006 at 6:16 pm

    On 6/23/06, Moon, John wrote:

    118 print "value
    =<$totals{$prev_fund}{$prev_service}{AMT}>\n";
    Can't use string ("3862926") as a HASH ref while "strict refs" in use at
    ./journal_items.pl line 118.
    From the looks of that message, I'm guessing that $totals{$prev_fund}
    holds 3862926 instead of a hash reference.
    154 $totals{$fund} += $amt;
    Here you're using $totals{$fund} as if it's an integer, instead of a
    hash reference. If it is a reference before line 154, it will be
    (silently) converted to an integer, since you're using it as one.
    Since the integer is a memory address, it's almost certain not to be
    what you want.

    Hope this helps!

    --Tom Phoenix
    Stonehenge Perl Training
  • Muma W. at Jun 23, 2006 at 8:53 pm

    Moon, John wrote:
    [...]
    150 $totals{$fund}{$service}{$acct}{AMT} += $amt;
    151 $totals{$fund}{$service}{$acct}{QTY} += $n_qty;
    152 $totals{$fund}{$service}{AMT} += $amt;
    153 $totals{$fund}{$service}{QTY} += $n_qty;
    154 $totals{$fund} += $amt;
    [...]
    I think this is the problem. There is a conflict between making
    $totals{$fund} a hash reference (lines 150-153) and making it a number
    (line 154).

    The same conflict exists between lines 150-151 and lines 152-153. I
    don't know much about your data, but I might do it like this:

    $totals{$fund}{$service}{$acct}{AMT} += $amt;
    $totals{$fund}{$service}{$acct}{QTY} += $n_qty;
    $totals{$fund}{$service}{total}{AMT} += $amt;
    $totals{$fund}{$service}{total}{QTY} += $n_qty;
    $totals{$fund}{total}{total}{total} += $amt;

    IOW, you (probably) need to keep the depths the same.

    HTH
  • Mr. Shawn H. Corey at Jun 23, 2006 at 9:04 pm

    On Fri, 2006-23-06 at 15:10 -0500, Muma W. wrote:
    IOW, you (probably) need to keep the depths the same.
    What he needs to do is to check the structure (with the debugger or
    Data::Dumper) every time before he uses the structure. And that means
    _every_ time. (Don't worry, like my karate instructor use to say, "The
    first hundred are easy.")


    --
    __END__

    Just my 0.00000002 million dollars worth,
    --- Shawn

    "For the things we have to learn before we can do them, we learn by doing them."
    Aristotle

    * Perl tutorials at http://perlmonks.org/?node=Tutorials
    * A searchable perldoc is at http://perldoc.perl.org/
  • Moon, John at Jun 25, 2006 at 2:05 pm
    From: Muma W.
    Sent: Friday, June 23, 2006 4:11 PM
    To: Beginners List
    Subject: Re: Can't use string ("3862926") as a HASH ref while "strict
    refs"

    Moon, John wrote:
    [...]
    150 $totals{$fund}{$service}{$acct}{AMT} += $amt;
    151 $totals{$fund}{$service}{$acct}{QTY} += $n_qty;
    152 $totals{$fund}{$service}{AMT} += $amt;
    153 $totals{$fund}{$service}{QTY} += $n_qty;
    154 $totals{$fund} += $amt;
    [...]
    I think this is the problem. There is a conflict between making
    $totals{$fund} a hash reference (lines 150-153) and making it a number
    (line 154).

    The same conflict exists between lines 150-151 and lines 152-153. I
    don't know much about your data, but I might do it like this:

    $totals{$fund}{$service}{$acct}{AMT} += $amt;
    $totals{$fund}{$service}{$acct}{QTY} += $n_qty;
    $totals{$fund}{$service}{total}{AMT} += $amt;
    $totals{$fund}{$service}{total}{QTY} += $n_qty;
    $totals{$fund}{total}{total}{total} += $amt;

    IOW, you (probably) need to keep the depths the same.

    HTH

    Thank you...

    jwm


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJun 23, '06 at 5:37p
activeJun 25, '06 at 2:05p
posts6
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase