Home > Archive > PERL Miscellaneous > August 2005 > need help with list of lists ( multi dimensional array)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
need help with list of lists ( multi dimensional array)
|
|
| news-acct 2005-08-25, 8:00 am |
| 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.
| |
| Paul Lalli 2005-08-25, 8:00 am |
| news-acct wrote:
> 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
| |
| Jim Gibson 2005-08-25, 6:57 pm |
| In article <1124974034.393055.114920@g47g2000cwa.googlegroups.com>,
Paul Lalli <mritty@gmail.com> wrote:
> news-acct wrote:
[problem description snipped]
[color=darkred]
>
> Why would you want to keep a separate count value? A Perl array in
> scalar context gives its size...
One reason would be to cut down on memory usage. There is no need to
keep identical entries in an array if all you want is to know how many
entries there are.
[sample program snipped]
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
| |
| news-acct 2005-08-27, 6:56 pm |
| "Paul Lalli" <mritty@gmail.com> wrote in message
news:1124974034.393055.114920@g47g2000cwa.googlegroups.com...
> news-acct wrote:
>
> perldoc perllol - "Lists of Lists"
> perldoc perldsc - "Data Structures Cookbook"
>
[ .. lines snipped ..]
> Paul Lalli
>
Thanks..
| |
| news-acct 2005-08-28, 3:56 am |
|
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
| |
| William James 2005-08-28, 7:56 am |
|
news-acct wrote:
> 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
| |
| Matt Garrish 2005-08-28, 7:56 am |
|
"William James" <w_a_x_man@yahoo.com> wrote in message
news:1125227941.943651.246270@g49g2000cwa.googlegroups.com...
>
> news-acct wrote:
>
> 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
| |
| A. Sinan Unur 2005-08-28, 6:56 pm |
| "Matt Garrish" <matthew.garrish@sympatico.ca> wrote in
news:m6iQe.56$884.16689@news20.bellglobal.com:
>
> "William James" <w_a_x_man@yahoo.com> wrote in message
> news:1125227941.943651.246270@g49g2000cwa.googlegroups.com...
....
>
> The Ruby group is down the hall. Or are so few people using the
> language you have to post here?
Nah, he's Xah Lee's long lost brother.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
|
|
|
|
|