need help with list of lists ( multi dimensional array)

N

news-acct

Hi - looking for pointers or links to resources ...

I am reading in a text file - lines are
prod_id cost sn
Prod1 1000 123
Product1 1000 124
Product1 2000 456
Prod1 2000 457
Product2 1000 323
Prod2 1000 324
Product2 2000 556
Prod2 2000 557
Product3 1000 223
Prod3 1000 224
Product3 2000 756

I need to count how many of each product, and how many of each product AND
cost.
Count of Product1 - 1000: 2
Count of Product1 - 2000: 2
Total count of Product1: 4

Count of Product2 - 1000: 2
Count of Product2 - 2000: 2
Total count of Product2: 4

Count of Produc3 - 1000: 2
Count of Product3 - 2000: 1
Total count of Product3: 3

Total count of all products: 11

It seems to be that an array would work great here, but I am not successful
at doing it.
would like to have an array -
array_cnt[Product_id][cost_value][count]
where I could just increment the [count] value.
Thanks.
 
P

Paul Lalli

news-acct said:
Hi - looking for pointers or links to resources ...

perldoc perllol - "Lists of Lists"
perldoc perldsc - "Data Structures Cookbook"
I am reading in a text file - lines are
prod_id cost sn
Prod1 1000 123
Product1 1000 124
Product1 2000 456
Prod1 2000 457
Product2 1000 323
Prod2 1000 324
Product2 2000 556
Prod2 2000 557
Product3 1000 223
Prod3 1000 224
Product3 2000 756

I need to count how many of each product, and how many of each product AND
cost.
Count of Product1 - 1000: 2
Count of Product1 - 2000: 2
Total count of Product1: 4

I don't understand your data. Is "Prod1" equivalent to "Product1"?
Count of Product2 - 1000: 2
Count of Product2 - 2000: 2
Total count of Product2: 4

Count of Produc3 - 1000: 2
Count of Product3 - 2000: 1
Total count of Product3: 3

Total count of all products: 11

It seems to be that an array would work great here, but I am not successful
at doing it.
would like to have an array -
array_cnt[Product_id][cost_value][count]
where I could just increment the [count] value.

Why would you want to keep a separate count value? A Perl array in
scalar context gives its size...

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my %products;
while (<DATA>){
my ($prod, $cost, $sn) = split ' ';
$prod =~ s/uct//;
push @{$products{$prod}{$cost}}, $sn;
}

for my $prod (sort keys %products){
my $total = 0;
for my $cost (sort {$a <=> $b} keys %{$products{$prod}}){
my $count = @{$products{$prod}{$cost}};
print "Count of $prod - $cost: $count\n";
$total += $count;
}
print "Total count of $prod: $total\n";
}


__DATA__
Prod1 1000 123
Product1 1000 124
Product1 2000 456
Prod1 2000 457
Product2 1000 323
Prod2 1000 324
Product2 2000 556
Prod2 2000 557
Product3 1000 223
Prod3 1000 224
Product3 2000 756


Output:
Count of Prod1 - 1000: 2
Count of Prod1 - 2000: 2
Total count of Prod1: 4
Count of Prod2 - 1000: 2
Count of Prod2 - 2000: 2
Total count of Prod2: 4
Count of Prod3 - 1000: 2
Count of Prod3 - 2000: 1
Total count of Prod3: 3

Paul Lalli
 
N

news-acct

trying to understand this better...
what if had 4 columns ( prod, cost, sn, loc_#)
___DATA___
prod1 10 123 B45
prod1 10 234 B45
prod1 20 456 C54
prod2 10 852 D55
prod2 10 853 D55
prod2 20 985 E54

Want total count of all prod's, and count of prod# and amt
prod1 10 count=2
prod1 20 count=1
prod2 10 count=2
prod2 20 count =1
Total count = 6

Are you using an array of hashes or hashes of arrays? Thanks
 
W

William James

news-acct said:
trying to understand this better...
what if had 4 columns ( prod, cost, sn, loc_#)
___DATA___
prod1 10 123 B45
prod1 10 234 B45
prod1 20 456 C54
prod2 10 852 D55
prod2 10 853 D55
prod2 20 985 E54

Want total count of all prod's, and count of prod# and amt
prod1 10 count=2
prod1 20 count=1
prod2 10 count=2
prod2 20 count =1
Total count = 6

Are you using an array of hashes or hashes of arrays? Thanks

Maybe this version in Ruby will be easier to understand.
It puts the information into a hash of hashes. After
reading the data, prodprice looks like:

{"prod1"=>{"20"=>1, "10"=>2}, "prod2"=>{"20"=>1, "10"=>2}}

-------------------------------------------------------------

prodprice = Hash.new { |hash, key| hash[key] = Hash.new(0) }

DATA.each { |line|
prod, price = line.split
prodprice[prod][price] += 1
}
sum = 0
prodprice.sort.each { |prod,hash|
hash.sort.each { |price,count|
puts "#{prod} #{price} #{count}"
sum += count
}
}
puts sum

__END__
prod1 10 123 B45
prod1 10 234 B45
prod1 20 456 C54
prod2 10 852 D55
prod2 10 853 D55
prod2 20 985 E54
 
M

Matt Garrish

William James said:
Maybe this version in Ruby will be easier to understand.

The Ruby group is down the hall. Or are so few people using the language you
have to post here?

I have an urge now to go find a C++ group and start posting C# answers to
everything. That'll learn them good!

Matt
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top