Array::Diff help understanding it

P

PB0711

First I apologise forr being so slow that I cannot understand this
simple module.
Here is the problem I have two Hash of Arrays (HoA's). I want to find
the difference between the arrays by useing Array::Diff. Array::Diff
askes for references to an array. Since I have

for $pro (keys $HoA_pro){
my $con = Array::Diff->diff($HoA_pro{$pro}, $HoA_gene{$gene});
print "$pro, $con->added\n";
}

It doesn't feel like I should be able to do that print but that's what
I'm getting from the README. When I print I'm getting a reference. Do I
need to unref it or ref it ......
 
P

Paul Lalli

PB0711 said:
First I apologise forr being so slow that I cannot understand this
simple module.

Not understanding does not warrant an apology.

Posting code with syntax errors does. Especially ones that make it
impossible to know what your *actual* problem is.
Here is the problem I have two Hash of Arrays (HoA's). I want to find
the difference between the arrays by useing Array::Diff. Array::Diff
askes for references to an array. Since I have

for $pro (keys $HoA_pro){

This is not valid Perl. keys takes a hash. Not a scalar. Either you
have a hash %HoA_pro, or a hash reference $HoA_pro (in which case you'd
need to dereference it).
my $con = Array::Diff->diff($HoA_pro{$pro}, $HoA_gene{$gene});

1) Where the heck did $gene come from?
2) Here's the problem that your syntax error above causes for me. I
don't know if you really have a hash, and therefore the first argumetn
is correct, or if you have a hash reference and the above argument is
the source of your problem. So I can't adequetly help you.

Please read the Posting Guidelines for this group. They encourage you
to post a SHORT but COMPLETE script that we can run by copy and
pasting, which demonstrates your error. If you had done this, it would
be far easier to help you, and you would get the help you want far
faster.

print "$pro, $con->added\n";
}

It doesn't feel like I should be able to do that print but that's what
I'm getting from the README.

I find that drastically unlikely. Please quote the relevant passage.
When I print I'm getting a reference. Do I
need to unref it or ref it ......

If you have a reference, you need to dereference it. Taking a
refernece to a reference is rarely a logical decision.

However, in this case, your problem is simply that method calls (like
any other subroutine calls) do not interpolate. You cannot just stick
them in a double quoted string.

Assuming added() is a method of the class Array::Diff (and I've not
looked into this module to determine if it is), the above should
become:
print "$pro, ", $con->added(), "\n";


Paul Lalli
 
P

Paul Lalli

Paul said:
Assuming added() is a method of the class Array::Diff (and I've not
looked into this module to determine if it is), the above should
become:
print "$pro, ", $con->added(), "\n";

After looking at the (rather disturbingly poor) documentation for the
module, it seems that added() likely returns a reference to an array
that contained the elements added in the new array from the old. If
that's the case, the above should become:
print "$pro, ", @{$con->added()}, "\n";

And actually, because arrays *do* interpolate whereas method calls do
not, you can put this back the way you had it:

print "$pro, @{$con->added()}\n";

Paul Lalli
 
P

PB0711

:) :) :)
Thank you for your helpful relply. I am happy to say that I am now on
my linux machine and not remembering my scribles from my paper and
posting from windoze.
"
while (my $line=<Prot>){
if ($line=~/<prot_desc>(.*)<\/prot_desc>/){ # finds protein name
$pro_name=$1;
@pep=-1;
}elsif($line=~/<pep_seq>(.*)<\/pep_seq>/){# finds pep sequence
@pep=uniq(@pep);
push (@pep, $1);
}
$HOA_protein{$pro_name}=[@pep]; # makes a HOA
}
#there is another one of these for @gene hense where it comes from
my $out_file="maggieXML.csv";
open OUT, ">$out_file" or die "Cannot write to file,$!\n";

for $pro_name(keys %HOA_protein){
my $con = Array::Diff->diff($HOA_protein{$pro_name},
$HOA_gene{$gene_name});
print OUT "$pro_name,@{$con->added()}\n";
print OUT "Present in Gene, @{$con->deleted()}\n";
}
"
Currently, with the line "print OUT "$pro_name,@{$con->added()}\n";" I
am still getting a reference as my output. If there is another module
that you can recommend for me I would be happy to use that as well.
 
P

PB0711

PB0711 said:
:) :) :)
Thank you for your helpful relply. I am happy to say that I am now on
my linux machine and not remembering my scribles from my paper and
posting from windoze.
"
while (my $line=<Prot>){
if ($line=~/<prot_desc>(.*)<\/prot_desc>/){ # finds protein name
$pro_name=$1;
@pep=-1;
}elsif($line=~/<pep_seq>(.*)<\/pep_seq>/){# finds pep sequence
@pep=uniq(@pep);
push (@pep, $1);
}
$HOA_protein{$pro_name}=[@pep]; # makes a HOA
}
#there is another one of these for @gene hense where it comes from
my $out_file="maggieXML.csv";
open OUT, ">$out_file" or die "Cannot write to file,$!\n";

for $pro_name(keys %HOA_protein){
my $con = Array::Diff->diff($HOA_protein{$pro_name},
$HOA_gene{$gene_name});
print OUT "$pro_name,@{$con->added()}\n";
print OUT "Present in Gene, @{$con->deleted()}\n";
}
"
Currently, with the line "print OUT "$pro_name,@{$con->added()}\n";" I
am still getting a reference as my output. If there is another module
that you can recommend for me I would be happy to use that as well.

Agh, put a space in between " print OUT "$pro_name,
@{$con->added()}\n"; " name and the @{$....

Thanks
 
P

Paul Lalli

PB0711 said:
:) :) :)
Thank you for your helpful relply.

You're welcome. You can best show your gratefulness by following my
requests in my first reply. First, read the Posting Guidelines, and
follow them. Specifically, Quote the material you are reply to - AFTER
trimming it down to the relevant bits, and beging your new post below
that material. Second, post a SHORT but COMPLETE script *that we can
run* by copy and pasting. Third, show the input and output of the
script. Without that information, I have no advice for you; I cannot
tell what parts of your script are logic errors, what parts are getting
bad data, etc.

Paul Lalli
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top