|
|
| Syntactic Confectionery Delight | |
| PerlMonks |
Re: Consolidating biological data into occurance of each biological unit per sample in tableby Cristoforo (Chaplain) |
| on Jan 14, 2013 at 16:11 UTC ( #1013237=note: print w/ replies, xml ) | Need Help?? |
|
I am new to programming and have been successfully manipulating my data in perl until now.Hello again ejohnston7! Considering your remark, (above), about being new to programming, it seems like the code I gave may have had some new and unfamiliar things in it that you may not understand. I know when I started Perl, I didn't get some of the common idioms that experienced programmers in Perl had been using. In this case, the use of map, (apply a change to a list to get back a new list with the changes), and the Hash of Hash data structure and how to access it. Here is the comma separated value approach, (below), and I'll try to follow it up with some explanation. If you have any questions, do come back and someone will try to explain what you ask about. Files created by the program above and readable by Excel are:
The first thing I'd like to do is provide a picture of what the %data hash contains using Data::Dumper. (I got this by placing the statement use Data::Dumper; print Dumper \%data; right after the while loop and before the for loop. I use Data::Dumper alot to see what exactly is in a data structure I created to see if everything is allright. I created a hash of a hash of a hash, (with this statement, $data{$type}{$sample}{$value}++ if $value;).
$type could be 'a' or 'c', (from your sample data). $sample is 'A', 'B' or 'C' and $value would be the name of the animal or the color. (Note that the statement ends with if $value;. In your explanation of the problem, you didn't want to count values that had no name.
There is no color here so it wouldn't be added to the hash. while (<DATA>) is shorthand for while (defined $_ = <DATA>). chomp with no argument chomps $_ by default. Likewise, split without an argument operates on $_ as well, split /[\t;]/. In the for loop, for (@fields), each element of the array being iterated over is assigned to $_, not the same $_ from the while loop but $_ localized to the for loop. They do not clash. Thats just some of the explanation, but enough to help you begin to understand hopefully. I have to leave now, but ask any questions about what you don't understand. Hope this explains a little for you.
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||