<?xml version="1.0" encoding="windows-1252"?>
<node id="1013237" title="Re: Consolidating biological data into occurance of each biological unit per sample in table" created="2013-01-14 11:11:38" updated="2013-01-14 11:11:38">
<type id="11">
note</type>
<author id="421781">
Cristoforo</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;I am new to programming and have been successfully manipulating my data in perl until now.&lt;/i&gt;&lt;/blockquote&gt;

Hello again [ejohnston7]!

&lt;p&gt;
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 [docs://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.
&lt;p&gt;
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.
&lt;c&gt;#!/usr/bin/perl
use strict;
use warnings;

my %data;
while (&lt;DATA&gt;) {
	chomp;
	my (undef, $sample, @fields) = split /[\t;]/;
	
	for (@fields) {
		my ($type, $value) = split /__/;
		$data{$type}{$sample}{$value}++ if $value;	
	}
}

for my $type (keys %data) {
	my $entity = $data{$type};
	my @samples = sort keys %$entity;
	
	my %seen;
	my @keys = grep !$seen{$_}++, map keys %$_, values %$entity;
	
	open my $fh, '&gt;', "$type.csv" or die "Unable to create '$type.csv'. $!";
	
	print $fh join(",", ' ', @samples), "\n";
	for my $key (@keys) {
		print $fh join(",", $key, map $entity-&gt;{$_}{$key} || 0, @samples), "\n";
	}
	
	close $fh or die "Unable to close '$type.csv'. $!";
}

__DATA__
occurence1	A	a__bear;c__black
occurence2	B	a__wolf;c__grey
occurence3	A	a__wolf;c__white
occurence4	A	a__bear;c__
occurence5	C	a__wolf;c__grey
occurence6	C	a__bear;c__brown
occurence7	A	a__wolf;c__
occurence8	B	a__wolf;c__
occurence9	C	a__bear;c__black
occurence10	C	a__wolf;c__
occurence11	A	a__wolf;c__red
occurence12	B	a__wolf;c__grey
occurence13	C	a__wolf;c__grey
occurence14	C	a__wolf;c__grey
occurence15	B	a__bear;c__brown
occurence16	C	a__bear;c__brown
occurence17	A	a__bear;c__
occurence18	A	a__bear;c__brown
occurence19	C	a__wolf;c__white
occurence20	B	a__wolf;c__grey
occurence21	B	a__bear;c__
occurence22	B	a__wolf;c__grey
occurence23	A	a__wolf;c__grey
occurence24	A	a__bear;c__brown
occurence25	C	a__bear;c__brown
occurence26	A	a__bear;c__brown
occurence27	C	a__bear;c__
occurence28	C	a__bear;c__brown
occurence29	B	a__wolf;c__red
occurence30	B	a__wolf;c__grey
&lt;/c&gt;

Files created by the program above and readable by Excel are:
&lt;c&gt;
C:\Old_Data\perlp&gt;type a.csv
 ,A,B,C
bear,6,2,6
wolf,4,7,5

C:\Old_Data\perlp&gt;type c.csv
 ,A,B,C
white,1,0,1
black,1,0,1
brown,3,1,4
red,1,1,0
grey,1,5,3
&lt;/c&gt;

&lt;p&gt;
The first thing I'd like to do is provide a picture of what the &lt;c&gt;%data&lt;/c&gt; hash contains using [mod://Data::Dumper]. (I got this by placing the statement &lt;c&gt;use Data::Dumper; print Dumper \%data;&lt;/c&gt; right after the &lt;i&gt;while&lt;/i&gt; loop and before the &lt;i&gt;for&lt;/i&gt; loop. I use Data::Dumper alot to see what exactly is in a data structure I created to see if everything is allright.
&lt;c&gt;
C:\Old_Data\perlp&gt;perl t5.pl
$VAR1 = {
          'c' =&gt; {
                   'A' =&gt; {
                            'white' =&gt; 1,
                            'black' =&gt; 1,
                            'brown' =&gt; 3,
                            'red' =&gt; 1,
                            'grey' =&gt; 1
                          },
                   'C' =&gt; {
                            'white' =&gt; 1,
                            'black' =&gt; 1,
                            'brown' =&gt; 4,
                            'grey' =&gt; 3
                          },
                   'B' =&gt; {
                            'red' =&gt; 1,
                            'brown' =&gt; 1,
                            'grey' =&gt; 5
                          }
                 },
          'a' =&gt; {
                   'A' =&gt; {
                            'bear' =&gt; 6,
                            'wolf' =&gt; 4
                          },
                   'C' =&gt; {
                            'bear' =&gt; 6,
                            'wolf' =&gt; 5
                          },
                   'B' =&gt; {
                            'bear' =&gt; 2,
                            'wolf' =&gt; 7
                          }
                 }
        };
&lt;/c&gt;
I created a hash of a hash of a hash, (with this statement, &lt;c&gt;$data{$type}{$sample}{$value}++ if $value;&lt;/c&gt;).
&lt;p&gt;
&lt;c&gt;$type&lt;/c&gt; could be 'a' or 'c', (from your sample data). &lt;c&gt;$sample&lt;/c&gt; is 'A', 'B' or 'C' and &lt;c&gt;$value&lt;/c&gt; would be the name of the animal or the color. (Note that the statement ends with &lt;c&gt;if $value;&lt;/c&gt;. In your explanation of the problem, you didn't want to count values that had no name.
&lt;br&gt;&lt;c&gt;occurence7    A    a__wolf;c__&lt;/c&gt;
&lt;p&gt;
There is no color here so it wouldn't be added to the hash.
&lt;p&gt;
&lt;c&gt;while (&lt;DATA&gt;)&lt;/c&gt; is shorthand for &lt;c&gt;while (defined $_ = &lt;DATA&gt;)&lt;/c&gt;.
&lt;p&gt; &lt;c&gt;chomp&lt;/c&gt; with no argument chomps &lt;c&gt;$_&lt;/c&gt; by default.
&lt;p&gt;Likewise, split without an argument operates on &lt;c&gt;$_&lt;/c&gt; as well, &lt;c&gt;split /[\t;]/&lt;/c&gt;.
&lt;p&gt;
In the for loop, &lt;c&gt;for (@fields)&lt;/c&gt;, each element of the array being iterated over is assigned to &lt;c&gt;$_&lt;/c&gt;, not the same &lt;c&gt;$_&lt;/c&gt; from the while loop but &lt;c&gt;$_&lt;/c&gt; localized to the for loop. They do not clash.
&lt;p&gt;
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.
&lt;p&gt; Hope this explains a little for you.</field>
<field name="root_node">
1013076</field>
<field name="parent_node">
1013076</field>
</data>
</node>
