hash of hashes

K

Kill Joy

Hi all..

Excuse since I'm a newbie...
I have an hash of hashes but there is something wrong in my code.


# If an item is found for the second time
if (exists $hash{ $codart }) {

# increase the quantity of the article $codart
$hash{ $codart }{ q }++;
print "\n$codart $qq EXISTS\n";

} else {

# insert the product
$hash{ $codart } = {
desc => $desc,
q => $quant,
price => $price };

}


How to do it?

Thanks.

Regards.

G.
 
W

Wolf

Am 24.03.2011 12:30, schrieb Kill Joy:
# insert the product
$hash{ $codart } = {
desc => $desc,
q => $quant,
price => $price }; <---- REMOVE ;

} <----APPEND ;

cheers, wolf
 
J

Justin C

Hi all..

Excuse since I'm a newbie...
I have an hash of hashes but there is something wrong in my code.


# If an item is found for the second time
if (exists $hash{ $codart }) {

# increase the quantity of the article $codart
$hash{ $codart }{ q }++;
print "\n$codart $qq EXISTS\n";

} else {

# insert the product
$hash{ $codart } = {
desc => $desc,
q => $quant,
price => $price };

}

"Global symbol $qq requires explicit package name at...

Have you tried:
use warnings;
use strict;

That will probably help you get your code to run, then you can ask what
it is you want.

Justin.
 
B

bobbi

Hi all..

Excuse since I'm a newbie...
I have an hash of hashes but there is something wrong in my code.

  # If an item is found for the second time
  if (exists  $hash{ $codart }) {

          #  increase the quantity of the article $codart
          $hash{ $codart }{ q }++;
          print "\n$codart $qq EXISTS\n";

  } else {

# insert the product
   $hash{ $codart } = {
    desc  => $desc,
    q    => $quant,
    price => $price };

   }

How to do it?

"Something wrong" is not a useful problem description.

Add:
use warnings;
use strict;
use Data::Dumper;

Report the output of:

print Dumper \%hash;

after doing some operations and explain why it is different from what
you expect.
 
B

bobbi

"Something wrong" is not a useful problem description.

Add:
use warnings;
use strict;
use Data::Dumper;

Report the output of:

print Dumper \%hash;

after doing some operations and explain why it is different from what
you expect.

Also, based on a wild guess at your code, don't you
want:
$hash{ $codart }{ q } += $quant;

instead of ++
 
K

Kill Joy

Hi all.

Many thanks for your previous replies.

I heva an Excel (XLS) file with this structure:

A C D E F G H I L M
N O
03/01/2011 CO 1 1 S 501.01902 CLIENT 100000 Prod A PZ 1
33,3300
03/01/2011 CO 1 2 S 501.01902 CLIENT 099000 Prod C PZ 1
12,5000
03/01/2011 CO 1 3 S 501.01902 CLIENT 088004 Prod B PZ 1
20,8300
03/01/2011 CO 1 4 S 501.01902 CLIENT 087003 Prod C PZ 1
12,5000
03/01/2011 CO 1 5 S 501.01902 CLIENT 088004 Prod B PZ 1
20,8300
03/01/2011 CO 1 6 S 501.01902 CLIENT 100000 Prod A PZ 1
33,3300
03/01/2011 CO 1 7 S 501.01902 CLIENT 099000 Prod C PZ 1
12,5000

I read it with
my $Excel = Win32::OLE->GetActiveObject('Excel.Application') ||
Win32::OLE->new('Excel.Application', 'Quit');

And I loop through it.

foreach my $row (1..4000)
{
foreach my $col (1..12)
{
# skip empty cells
next unless defined $Sheet->Cells($row,$col)->{'Value'};

# I need fields I, N, L and O
if ($col == 8) {

$codart = $Sheet->Cells($row,$col)->{'Formula'};

} elsif ($col == 9) {

$desc = $Sheet->Cells($row,$col)->{'Formula'};

} elsif ($col == 10) {

$quant = $Sheet->Cells($row,$col)->{'Formula'};

} elsif ($col == 11) {

$price = $Sheet->Cells($row,$col)->{'Formula'};
}


my $qq;

if (exists $hash{ $codart }) {
# I want to count the occurrences of $hash{ $codart }
$hash{ $codart }{ q } += $quant; # increment the quantity of
this article
print "\n$codart: $hash{ $codart }{ q } EXISTS\n";
} else {

# creare the new article
$hash{ $codart } = {
desc => $desc,
quant => $quant,
price => $price
};

}

} # col for
} # row for


I want a data structure
like this

$VAR1 = {
'100000' => {
'price' => '33.3',
'quant' => 2,
'desc' => 'Prod A'},

{
'088004' => {
'price' => '20.83',
'quant' => 2,
'desc' => 'Prod B'},

and so on...

How to do it?

Many thanks.

Regards.

Gius.
 
G

George Mpouras

Στις 24/3/2011 5:53 μμ, ο/η Kill Joy έγÏαψε:
A C D E F G H I L M
N O
03/01/2011 CO 1 1 S 501.01902 CLIENT 100000 Prod A PZ 1
33,3300
03/01/2011 CO 1 2 S 501.01902 CLIENT 099000 Prod C PZ 1
12,5000
03/01/2011 CO 1 3 S 501.01902 CLIENT 088004 Prod B PZ 1
20,8300
03/01/2011 CO 1 4 S 501.01902 CLIENT 087003 Prod C PZ 1
12,5000
03/01/2011 CO 1 5 S 501.01902 CLIENT 088004 Prod B PZ 1
20,8300
03/01/2011 CO 1 6 S 501.01902 CLIENT 100000 Prod A PZ 1
33,3300
03/01/2011 CO 1 7 S 501.01902 CLIENT 099000 Prod C PZ 1
12,5000









#!/usr/bin/perl
use Data::Dumper;$Data::Dumper::Terse=1;$Data::Dumper::Deepcopy=1;
my %hash;

while(<DATA>) {
chomp;$_=[split /\|/];
$hash{$_->[7]}={
'price' => $_->[11],
'quant' => $_->[2],
'desc' => $_->[8]};
}

print Dumper(\%hash);

__DATA__
03/01/2011|CO|1|1|S|501.01902|CLIENT|100000|Prod A|PZ|1|33,3300
03/01/2011|CO|1|2|S|501.01902|CLIENT|099000|Prod C|PZ|1|12,5000
03/01/2011|CO|1|3|S|501.01902|CLIENT|088004|Prod B|PZ|1|20,8300
03/01/2011|CO|1|4|S|501.01902|CLIENT|087003|Prod C|PZ|1|12,5000
03/01/2011|CO|1|5|S|501.01902|CLIENT|088004|Prod B|PZ|1|20,8300
03/01/2011|CO|1|6|S|501.01902|CLIENT|100000|Prod A|PZ|1|33,3300
03/01/2011|CO|1|7|S|501.01902|CLIENT|099000|Prod C|PZ|1|12,5000
 
J

Justin C

Hi all.

Many thanks for your previous replies.

I heva an Excel (XLS) file with this structure:

You appear to have something quite close to what you want. What is it
doing that it shouldn't, what isn't it doing that it should?

I've no experience with the module you're using, I've had a lot of
success with Spreadsheet::parseExcel if the module is not working as
you'd like it to.

Justin.
 
B

bobbi

Hi all.

Many thanks for your previous replies.

I heva an Excel (XLS) file with this structure:

A          C  D E F G         H         I      L           M
N      O
03/01/2011 CO 1 1 S 501.01902 CLIENT 100000     Prod A      PZ    1
33,3300
03/01/2011 CO 1 2 S 501.01902 CLIENT 099000     Prod C      PZ    1
12,5000
03/01/2011 CO 1 3 S 501.01902 CLIENT 088004     Prod B      PZ    1
20,8300
03/01/2011 CO 1 4 S 501.01902 CLIENT 087003     Prod C      PZ    1
12,5000
03/01/2011 CO 1 5 S 501.01902 CLIENT 088004     Prod B      PZ    1
20,8300
03/01/2011 CO 1 6 S 501.01902 CLIENT 100000     Prod A      PZ    1
33,3300
03/01/2011 CO 1 7 S 501.01902 CLIENT 099000     Prod C      PZ    1
12,5000

I read it with
my $Excel = Win32::OLE->GetActiveObject('Excel.Application') ||
Win32::OLE->new('Excel.Application', 'Quit');

And I loop through it.

foreach my $row (1..4000)
{
 foreach my $col (1..12)
 {
  # skip empty cells
  next unless defined $Sheet->Cells($row,$col)->{'Value'};

   # I need fields I, N, L and O
  if ($col == 8) {

   $codart =  $Sheet->Cells($row,$col)->{'Formula'};

   } elsif ($col == 9) {

   $desc =  $Sheet->Cells($row,$col)->{'Formula'};

   } elsif ($col == 10) {

   $quant =  $Sheet->Cells($row,$col)->{'Formula'};

   } elsif ($col == 11) {

   $price =  $Sheet->Cells($row,$col)->{'Formula'};
   }

my $qq;

  if (exists  $hash{ $codart }) {
          # I want to count the occurrences of $hash{ $codart }
          $hash{ $codart }{ q } += $quant; # increment the quantity of
this article
          print "\n$codart: $hash{ $codart }{ q } EXISTS\n";
  } else {

     # creare the new article
   $hash{ $codart } = {
    desc  => $desc,
    quant    => $quant,
    price => $price
    };

  }

 } # col for

} # row for

I want a data structure
like this

$VAR1 = {
          '100000' => {
            'price' => '33.3',
            'quant' => 2,
            'desc' => 'Prod A'},

{
          '088004' => {
            'price' => '20.83',
            'quant' => 2,
            'desc' => 'Prod B'},

and so on...

How to do it?

Many thanks.

Regards.

Gius.

Still not quite sure what you are doing with quant, qq, q:
# I want to count the occurrences of $hash{ $codart }

Are you adding the number from the spreadsheet or counting
the number of times you see the same codart?
If both you need two hash keys:

exists:
$hash{ $codart }{ q }++;
$hash{ $codart }{ quant } += $quant;

1st time:
# create the new article
$hash{ $codart } = {
desc => $desc,
quant => $quant, # total quantity
q => 1, # number of occurrences
price => $price,
};
 
C

ccc31807

I have an hash of hashes but there is something wrong in my code.

Here is some code and output. I put the data in a tab delimited file,
which you can do with Excel. You can format the output how you want. I
would also validate your data to check and see if the price and
description is the same for each unique product ID, which I didn't do
here.

CODE:
#! perl
use strict;
use warnings;

my (%products, %quan);
while(<DATA>)
{
next unless /\w/;
chomp;
my($f0, $f1, $f2, $f3, $f4, $f5, $f6, $prod_id, $prod_desc, $f7,
$prod_quan, $prod_price) = split(/\t/, $_);
#print "$prod_id, $prod_desc, $prod_quan, $prod_price\n";
$products{$prod_id} = {
prod_desc => $prod_desc,
prod_price => $prod_price,
};
$quan{$prod_id} += $prod_quan;
}

foreach my $k (sort keys %products)
{
print "PRODUCT ID: $k\n";
foreach my $k2 (sort keys %{$products{$k}})
{
print " $k2: $products{$k}{$k2}\n";
}
print " quantity: $quan{$k}\n";
}
exit(0);

__DATA__
03/01/2011 CO 1 1 S 501.01902 CLIENT 100000 Prod A PZ 1 33,3300
03/01/2011 CO 1 2 S 501.01902 CLIENT 099000 Prod C PZ 1 12,5000
03/01/2011 CO 1 3 S 501.01902 CLIENT 088004 Prod B PZ 1 20,8300
03/01/2011 CO 1 4 S 501.01902 CLIENT 087003 Prod C PZ 1 12,5000
03/01/2011 CO 1 5 S 501.01902 CLIENT 088004 Prod B PZ 1 20,8300
03/01/2011 CO 1 6 S 501.01902 CLIENT 100000 Prod A PZ 1 33,3300
03/01/2011 CO 1 7 S 501.01902 CLIENT 099000 Prod C PZ 1 12,5000

OUTPUT:
PRODUCT ID: 087003
prod_desc: Prod C
prod_price: 12,5000
quantity: 1
PRODUCT ID: 088004
prod_desc: Prod B
prod_price: 20,8300
quantity: 2
PRODUCT ID: 099000
prod_desc: Prod C
prod_price: 12,5000
quantity: 2
PRODUCT ID: 100000
prod_desc: Prod A
prod_price: 33,3300
quantity: 2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top