http://qs1969.pair.com?node_id=336272

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am having trouble understanding a part of program that I am trying to tweak that I got off the web for sequence alignment.

I haven't changed anything but keep getting errors for

while (scalar(@ra) while (scalar(@rb) while (scalar(@rc) # print out the answer

It is in the context of

if (!$d->[$x][$y][0] && !$d->[$x][$y][1] && !$d->[$x][$y][2]) { for (my $u=$x; $u>0; $u--) { unshift @ra, $sa[$u-1]; } for (my $v=$y; $v>0; $v--) { unshift @rb, $sb[$v-1]; } while (scalar(@ra) while (scalar(@rb) while (scalar(@rc) # print out the ans +wer $i++; # global variable $scr = 0; # score for each alignment print "<br>Alignment: $i<br>Sa:"; foreach my $x (@ra) { print " $x"; } + print "<br>Sb:"; foreach my $y (@rb) { print " $y"; } + print "<br>Scr"; foreach my $z (@rc) { if ($z eq '.') { print " ."; } else { printf '%4d', $z; $scr += $z; } } print " = $scr<br>"; }

What does it mean? And why do I keep getting errors? #I know that scalar() is to force scalar context.#

Thanks Monks, chris

Replies are listed 'Best First'.
Re: while (scalar()
by Happy-the-monk (Canon) on Mar 12, 2004 at 21:58 UTC

    this is a "blind" answer: I see your parentheses aren't equally weighted.
    Keep counting them for every instruction. Open one, close one. Open two, close two.
    Finish each instruction with a semicolon ";" or closing "}" if there was an opening"{" before.

    When you've put that right, and the errors persist, tell us what the errors actually say. Most times, they are worth being read.

    Sören

      while (scalar(@ra)) while (scalar(@rb)) while (scalar(@rc))
      After I put in the parentheses, the error is: syntax error at local_align.cgi line 89, near ") while"

      I am not totally new to perl, but this construction makes NO sense to me at all. I was curious if it was some PERL shorthand or something.

        for, while, do, if, until, unless all operate on BLOCKS of code. If you come from C you will be safe to assume that you always need  { .... } and you always need ; Anyway the general sysntax is

        KEYWORD ( CONDITION ) { # begining of block of code to exec marked by { # stuff you want to do goes in here } # end of block of code to exec marked by }

        So in short your code is a syntax error(s). Your indentation is also shot. It should look like:

        if ( $some_condition ) { # some_condition is true while( @something && @something_else ) { shift @something; pop @something_else; } } else { print "Some condition is false!\n"; }

        Code like my second example will compile and is easy to read. Your insistence that nothing has changed means either it never worked, someone rediefined the perl languague while we were not looking or you have changed stuff.

        It looks to me as if you are trying to maintain a piece of code written by someone else. I would suggest for a start you google for perltidy and install it, then run it on your code to clean the indentation into something reasonable. That will make your life a lot easier.

        cheers

        tachyon

        it makes no sense. It is wrong.

        As to see what makes sense:
        scalar(@ra)   counts the elements of array @ra.
        while ( scalar( @ra ) )   does a while condition around the counting. In this case, you would normally just leave out the counting and write   while ( @ra )
        Anyway, there needs to be a { } block as to which the while condition belongs. It is missing, that's an error I can't make any sense of.

        Sören

Re: while (scalar()
by bageler (Hermit) on Mar 13, 2004 at 00:05 UTC
    that's an incomplete code block. if it were complete, somehow, it would be an infinite loop if there are contents in @ra,@rb, or @rc since no arguments are ever popped or shifted out of the arrays. either it's completely broken or the context which you provide is very incomplete.

    also, the code formatting is horrendous!
Re: while (scalar()
by TilRMan (Friar) on Mar 12, 2004 at 22:29 UTC

    Perhaps when you downloaded the script (or when it was uploaded) a less-than (<) got mistaken for the start of an HTML tag? Download again and make sure what you downloaded runs properly. I suggest you pertidy the script before you start tweaking it.

    -- 
    LP^>

Re: while (scalar()
by esskar (Deacon) on Mar 12, 2004 at 23:05 UTC
    forget that code
    do not learn from things that do not work and do not make any sense...
    sorry
Re: while (scalar()
by neniro (Priest) on Mar 12, 2004 at 22:14 UTC
    I cant't find the place where you pop or shift elements from your arrays, so your while-loops are infinite.
Re: while (scalar()
by esskar (Deacon) on Mar 12, 2004 at 21:56 UTC
    while (scalar(@ra)) { while (scalar(@rb)) { while (scalar(@rc)) { # rest } } }
    but it doesn't make sense anyway
Re: while (scalar()
by Anonymous Monk on Mar 12, 2004 at 22:36 UTC
    I really hope that this is the workings of a malevolent trolling genius.