Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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, beginners-digest-help@perl.org wrote:

> From: Gunnar Hjalmarsson <noreply@gunnar.cc>
> Date: September 19, 2004 9:12:32 PM MDT
> To: beginners@perl.org
> Subject: 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
> 
>
> 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
>
>


Report this thread to moderator Post Follow-up to this message
Old Post
Michael Robeson
09-24-04 08:59 PM


Re: Moving between hashes 2.
Michael Robeson wrote:
> I decided to use the if statement you posted:

<snip>

> 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.

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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
09-24-04 08:59 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:26 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.