Re: nested "if"

2004-07-03 Thread Michael S. Robeson II
Well yeah, the indentation makes it much more clearer. However, this 
does not help me understand how the nested "if" statements are working. 
Which of the two "if" statements gets evaluated first? I am trying to 
figure out "in english" what the "if" statements are actually doing. Is 
it saying:

"If a line begins with  ">bla-bla"  and if  $seq  (which appears no 
where else in the code other than " $seq="" ") exists assign it to the 
hash "pro" with the name "bla-bla"."

So my question is how does the inner if statement work when seq="" is 
out side that "if" statement?

 Is the outer  "if" statement evaluated first then the inner? Because 
how does the inner "if" statement know what "$seq" is?

I am probably not making any sense but I am trying to figure out 
mechanically how the perl interpreter knows what to do in the context 
of the nested if statements.

-Thanks
-Mike
Gunnar Hjalmarsson wrote:
That illustrates the importance of indenting the code in a way that 
makes sense:

while () {
$line=$_;
if ($line=~/^>(.+)/) {
if ($seq) {
$pro{$name}=$seq;
#print "SEQ:\n$pro\n\n";
}
$name=$1;
$name=~s/\s//g;
push @names, $name;
#print "$name\n";
$k++;
$seq="";
} else {
chomp $line;
$seq.=$line;
}
}
Quite a difference, isn't it?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: nested "if"

2004-07-03 Thread Randy W. Sims
On 7/2/2004 10:25 PM, Michael S. Robeson II wrote:
Well yeah, the indentation makes it much more clearer. However, this 
does not help me understand how the nested "if" statements are working. 
Which of the two "if" statements gets evaluated first? I am trying to 
figure out "in english" what the "if" statements are actually doing. Is 
it saying:

"If a line begins with  ">bla-bla"  and if  $seq  (which appears no 
where else in the code other than " $seq="" ") exists assign it to the 
hash "pro" with the name "bla-bla"."

So my question is how does the inner if statement work when seq="" is 
out side that "if" statement?

 Is the outer  "if" statement evaluated first then the inner? Because 
how does the inner "if" statement know what "$seq" is?

I am probably not making any sense but I am trying to figure out 
mechanically how the perl interpreter knows what to do in the context of 
the nested if statements.
Tidied up a little more:
my( %pro, @names);
my( $name, $seq, $k );
while (defined( my $line =  )) {
if ($line =~ /^>(.+)/) {
if ($seq) {
$pro{$name} = $seq;
$seq = '';
}
$name = $1;
$name =~ s/\s//g;
push @names, $name;
$k++;
} else {
chomp( $line );
$seq .= $line;
}
}
This code deals with multi-line sequences, putting multiple lines 
together untill a sequence is complete. The 'else' part of the outter 
'if' does the accumulation of multiple lines into a sequence. The 'if' 
part determines that a sequence is complete, captures some type of name 
from the sequence, stores the complete sequence in the '%pro' hash, and 
pushes the name onto a '@names' array. I'm guessing '$k' keeps tally of 
the number of sequences; it's not clear if that is neccessary since 
`scalar @names` possibly will provide the same info. It's also unclear 
why there is a '@names' array that mostly duplicates `keys %pro`

I think where you're-understandably-getting confused is that most of 
those variables are global. That's made more explicit in my strictified 
rewrite above. It could probably be rewritten better if we knew the 
exact format of the data being read.

Randy.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: nested "if"

2004-07-03 Thread Randy W. Sims
On 7/3/2004 4:38 AM, Randy W. Sims wrote:
Tidied up a little more:
Actually, I'd probably invert the condition to make it clearer that it's 
accumulating multi-line sequences.

my( %pro, @names);
my( $name, $seq, $k );
while (defined( my $line =  )) {
unless ($line =~ /^>(.+)/) {
chomp( $line );
$seq .= $line;
} else {
if ($seq) {
$pro{$name} = $seq;
$seq = '';
}
($name = $1) =~ s/\s//g;
push @names, $name;
$k++;
}
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: nested "if"

2004-07-03 Thread Gunnar Hjalmarsson
Michael S. Robeson II wrote:
Which of the two "if" statements gets evaluated first? I am trying
to figure out "in english" what the "if" statements are actually
doing. Is it saying:
"If a line begins with  ">bla-bla"  and if  $seq  (which appears no
where else in the code other than " $seq="" ") exists assign it to
the hash "pro" with the name "bla-bla"."
So my question is how does the inner if statement work when seq=""
is out side that "if" statement?
It's a while loop. For a particular iteration of the loop, $seq and
$name may contain values that were assigned to them during a
*previous* iteration, but the values that will be used in the inner
if-statement during the *current* iteration is not related to what
will happen to those variables later during this same iteration.
Is the outer  "if" statement evaluated first then the inner?
As long as we are talking about the same iteration: No.
Because how does the inner "if" statement know what "$seq" is?
See above.
HTH
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: unique array

2004-07-03 Thread Randal L. Schwartz
> "Perl" == Perl Org <[EMAIL PROTECTED]> writes:

Perl> Several people responded individually with comments like this.
Perl> If the authors care, shouldn't they contact the ISP?  I mean, I
Perl> can't police the internet single-handedly.

No, but you can have enough sense that if the ENTIRE WORKS of authors
like me are *online*, someone is doing something wrong.

Please, don't tell me you sat there and had ABSOLUTELY NO CLUE.
What color is the sky on the planet where you come from?

So, even if you don't want to forward that to [EMAIL PROTECTED] (the
proper thing to do), the second best thing is REMOVE THAT LINK FROM
YOUR BOOKMARKS, and NOT SHARE IT?

Get it?

Stop taking money out of my pocket.  You offend me.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: nested "if"

2004-07-03 Thread Michael S. Robeson II
Ok, that make much more sense - I think. So, I guess, the outer 'if' 
and 'else' statements get evaluated first. Then the inner 'if' can 
proceed once all the lines of data were gathered in the outer 'else' 
statement. This way the lines can be assigned as a key-value pair in 
the hash. I guess the individual who wrote the code could make it much 
cleaner or easier to read. The re-organizing of the conditionals in 
your second e-mail makes it perfectly clear and would be something I 
would have done had I known how the nested 'if' statement was working 
(again if I have right). Hopefully, I 'got it' now. I can see why many 
coders are annoyed with nested if statements.  :-)

-Thanks!
-Mike

On Jul 3, 2004, at 4:38 AM, Randy W. Sims wrote:
On 7/2/2004 10:25 PM, Michael S. Robeson II wrote:
Well yeah, the indentation makes it much more clearer. However, this 
does not help me understand how the nested "if" statements are 
working. Which of the two "if" statements gets evaluated first? I am 
trying to figure out "in english" what the "if" statements are 
actually doing. Is it saying:
"If a line begins with  ">bla-bla"  and if  $seq  (which appears no 
where else in the code other than " $seq="" ") exists assign it to 
the hash "pro" with the name "bla-bla"."
So my question is how does the inner if statement work when seq="" is 
out side that "if" statement?
 Is the outer  "if" statement evaluated first then the inner? Because 
how does the inner "if" statement know what "$seq" is?
I am probably not making any sense but I am trying to figure out 
mechanically how the perl interpreter knows what to do in the context 
of the nested if statements.
Tidied up a little more:
my( %pro, @names);
my( $name, $seq, $k );
while (defined( my $line =  )) {
if ($line =~ /^>(.+)/) {
if ($seq) {
$pro{$name} = $seq;
$seq = '';
}
$name = $1;
$name =~ s/\s//g;
push @names, $name;
$k++;
} else {
chomp( $line );
$seq .= $line;
}
}
This code deals with multi-line sequences, putting multiple lines 
together untill a sequence is complete. The 'else' part of the outter 
'if' does the accumulation of multiple lines into a sequence. The 'if' 
part determines that a sequence is complete, captures some type of 
name from the sequence, stores the complete sequence in the '%pro' 
hash, and pushes the name onto a '@names' array. I'm guessing '$k' 
keeps tally of the number of sequences; it's not clear if that is 
neccessary since `scalar @names` possibly will provide the same info. 
It's also unclear why there is a '@names' array that mostly duplicates 
`keys %pro`

I think where you're-understandably-getting confused is that most of 
those variables are global. That's made more explicit in my 
strictified rewrite above. It could probably be rewritten better if we 
knew the exact format of the data being read.

Randy.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]