Professional Web Applications Themes

Moving between hashes 2. - PERL Beginners

Ok, well I think I can see the forest but I have little idea as to what is actually going on here. I spent a few hours looking things up and I have a general sense of what is actually occurring but I am getting lost in the details that were posted in the last digest. See below: On Sep 19, 2004, at 10:08, org wrote:   I have never seen anything like this nor can I find anything in any of my Perl books to help me explain what the 0,1 and the " are doing to the substr ...

  1. #1

    Default Moving between hashes 2.

    Ok, well I think I can see the forest but I have little idea as to what
    is actually going on here. I spent a few hours looking things up and I
    have a general sense of what is actually occurring but I am getting
    lost in the details that were posted in the last digest. See below:


    On Sep 19, 2004, at 10:08, org wrote:
     

    I have never seen anything like this nor can I find anything in any of
    my Perl books to help me explain what the 0,1 and the " are doing to
    the substr of $hash1. I assume it is position information of some kind?
    If so, what is going on?
     

    This is something new to me. I think I follow your use of the ?:
    pattern feature. However, none of the perl books I have discuss it's
    use in this fashion. So, I am unsure of how you know to do that, or
    rather... how would I have known that I can do that? But basically I
    see that you are looking for '-' and equating it with what is matching
    between the ? and : (i.e. '---').

    So, as far as I can tell, you are saying: "hey, if you find '-' in $aa
    then append a '---' in $hash3, otherwise append the next three DNA
    letters". However, I do not understand the syntax of how perl is
    actually doing this.

    Help with explanation would be greatly appreciated. As you can see I
    can see what the big picture is, it's just that I am unable to
    determine mechanistically how perl is actually going about doing it.
    Also, any online references to the techniques used above would be
    great. I'd look for them myself but I do not know what some of these
    are actually called?

    -Thanks so much, I have learned a little just from this much so far.
    -mike

    Michael Guest

  2. #2

    Default Re: Moving between hashes 2.

    Michael S. Robeson II wrote: 

    Well, before an attempt to explain and/or point you to the applicable
    docs, I'd like to change my mind once again. :) This is my latest
    idea:

    my %hash3;
    for ( keys %hash1 ) {
    my $dna = $hash2{$_};
    for my $aa ( split //, $hash1{$_} ) {
    $hash3{$_} .= $aa eq '-' ? '---' : substr $dna,0,3,'';
    }
    }

    I'll assume that you don't have a problem with the outer loop, that
    simply iterates over the hash keys. As a first step in each iteration
    I copy the DNA sequence to the $dna variable, so as to not destroying
    %hash2.

    Over to the 'tricky' part. The inner loop iterates over each character
    in the amino-acid sequence data, and respective character is assigned
    to $aa. For that I use the split() function:
    http://www.perldoc.com/perl5.8.4/pod/func/split.html
     
    >
    > This is something new to me. I think I follow your use of the ?:
    > pattern feature. However, none of the perl books I have discuss
    > it's use in this fashion.[/ref]

    That sounds strange to me, because that's how it should be used...
    Read about the conditional operator in
    http://www.perldoc.com/perl5.8.4/pod/perlop.html

    OTOH, that notation is basically the same as:

    if ( $aa eq '-' ) {
    $hash3{$_} .= '---';
    } else {
    $hash3{$_} .= substr $dna,0,3,'';
    }

    which is a little more intuitive (at least I think it is).
     

    Precisely.
     

    Hopefully the if/else statement makes it easier to grasp, and the '.='
    operator is used just for appending something to a string.

    Finally we have my use of the substr() function.
    http://www.perldoc.com/perl5.8.4/pod/func/substr.html
    It returns the first three characters in $dna, and since I also pass
    the null string as the fourth argument, it changes the content of $dna
    at the same time, i.e. it replaces the first three characters with
    nothing.

    HTH. If you need further explanations, you'll have to ask specific
    questions.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  3. #3

    Default Moving between hashes 2.

    Gunnar,

    Thanks so much for the help and the links! They help quit a bit. I
    decided to use the if statement you posted:

    if ( $aa eq '-' ) {
    $hash3{$_} .= '---';
    } else {
    $hash3{$_} .= substr $dna,0,3,'';
    }

    instead of:

    $hash3{$_} .= $aa eq '-' ? '---' : substr $dna,0,3,'';

    only because I had to add a $count++ function within the else statement
    (shown below) to accomplish another task within my larger script:

    if ( $aa eq '-' ) {
    $hash3{$_} .= '---';
    } else {
    $hash3{$_} .= substr $dna,0,3,'';
    $count++
    }

    I couldn't figure out if it was possible to add $count++ within the ?:
    statement above. I tried but could not get it to work.

    However, everything works well at this point. Again, I really
    appreciate the help!

    -Mike

    On Sep 20, 2004, at 6:55 PM, org wrote:
     
    >
    > Well, before an attempt to explain and/or point you to the applicable
    > docs, I'd like to change my mind once again. :) This is my latest
    > idea:
    >
    > my %hash3;
    > for ( keys %hash1 ) {
    > my $dna = $hash2{$_};
    > for my $aa ( split //, $hash1{$_} ) {
    > $hash3{$_} .= $aa eq '-' ? '---' : substr $dna,0,3,'';
    > }
    > }
    >
    > I'll assume that you don't have a problem with the outer loop, that
    > simply iterates over the hash keys. As a first step in each iteration
    > I copy the DNA sequence to the $dna variable, so as to not destroying
    > %hash2.
    >
    > Over to the 'tricky' part. The inner loop iterates over each character
    > in the amino-acid sequence data, and respective character is assigned
    > to $aa. For that I use the split() function:
    > http://www.perldoc.com/perl5.8.4/pod/func/split.html

    >> This is something new to me. I think I follow your use of the ?:
    >> pattern feature. However, none of the perl books I have discuss
    >> it's use in this fashion.[/ref]
    >
    > That sounds strange to me, because that's how it should be used...
    > Read about the conditional operator in
    > http://www.perldoc.com/perl5.8.4/pod/perlop.html
    >
    > OTOH, that notation is basically the same as:
    >
    > if ( $aa eq '-' ) {
    > $hash3{$_} .= '---';
    > } else {
    > $hash3{$_} .= substr $dna,0,3,'';
    > }
    >
    > which is a little more intuitive (at least I think it is).

    >
    > Precisely.

    >
    > Hopefully the if/else statement makes it easier to grasp, and the '.='
    > operator is used just for appending something to a string.
    >
    > Finally we have my use of the substr() function.
    > http://www.perldoc.com/perl5.8.4/pod/func/substr.html
    > It returns the first three characters in $dna, and since I also pass
    > the null string as the fourth argument, it changes the content of $dna
    > at the same time, i.e. it replaces the first three characters with
    > nothing.
    >
    > HTH. If you need further explanations, you'll have to ask specific
    > questions.
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    >
    >[/ref]

    Michael Guest

  4. #4

    Default Re: Moving between hashes 2.

    Michael Robeson wrote: 

    <snip>
     

    Right, the conditional operator is merely designed for assignment.

    OTOH, you don't need a loop to count a certain type of characters in a
    string:

    my $string = 'mfg--f';
    my $count = $string =~ tr/a-z//;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

Similar Threads

  1. Getting data from hashes
    By Robin Sheat in forum PERL Beginners
    Replies: 1
    Last Post: January 22nd, 06:47 AM
  2. Moving Randomly Moving Sprite To New Location on mouseEnter
    By ScottE in forum Macromedia Director Lingo
    Replies: 9
    Last Post: November 8th, 01:27 PM
  3. hashes
    By John in forum PERL Miscellaneous
    Replies: 13
    Last Post: October 3rd, 09:24 AM
  4. Hashes
    By Tore Aursand in forum PERL Beginners
    Replies: 2
    Last Post: September 8th, 08:00 PM
  5. I need help using %Hashes
    By Cowboy in forum PERL Beginners
    Replies: 0
    Last Post: August 20th, 07:03 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139