Array::Diff help understanding it

Discussion in 'Perl Misc' started by PB0711, Aug 9, 2006.

  1. PB0711

    PB0711 Guest

    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 ......
     
    PB0711, Aug 9, 2006
    #1
    1. Advertising

  2. PB0711

    Paul Lalli Guest

    PB0711 wrote:
    > 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
     
    Paul Lalli, Aug 9, 2006
    #2
    1. Advertising

  3. PB0711

    Paul Lalli Guest

    Paul Lalli wrote:

    > 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
     
    Paul Lalli, Aug 9, 2006
    #3
  4. PB0711

    PB0711 Guest

    :) :) :)
    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.
     
    PB0711, Aug 9, 2006
    #4
  5. PB0711

    PB0711 Guest

    PB0711 wrote:
    > :) :) :)
    > 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
     
    PB0711, Aug 9, 2006
    #5
  6. PB0711

    Paul Lalli Guest

    PB0711 wrote:
    > :) :) :)
    > 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
     
    Paul Lalli, Aug 9, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    532
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    681
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,980
    Steve Fulton
    Aug 31, 2004
  4. Replies:
    4
    Views:
    317
    Jack Klein
    Sep 26, 2006
  5. Austin Ziegler

    [ANN] Diff::LCS 1.1.0, Diff::LCS 1.0.4

    Austin Ziegler, Aug 8, 2004, in forum: Ruby
    Replies:
    3
    Views:
    213
    Austin Ziegler
    Aug 9, 2004
Loading...

Share This Page