in reply to Simple loop question

You are using "real" values, but what you really need is "string" values:

$x = 1; # integer $y = 6; # integer for( 1 .. 10 ) { $start_pt="$x.$y"; # string print " $start_pt "; $y++; } # 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 1.15

Replies are listed 'Best First'.
Re: Re: Simple loop question
by perl_seeker (Scribe) on Aug 01, 2003 at 11:32 UTC
    Hello,

    thanks a lot for the reply.That seems to be a nice
    and simple solution to the problem.I'll try it out.
    Thanx
Re: Re: Simple loop question
by perl_seeker (Scribe) on Aug 06, 2003 at 11:23 UTC
    Hi,
    Here's my changed code(part of it).

    foreach $mitem(@missing) { $result=$t->search(-forwards,"**",'end'); #print"\nThe start pos of the pattern:"; #print "\n$result"; #1.4 $start=$result+.2; #print "\n$start"; #1.6 $x=substr($start,0,1);# integer $y = substr($start,2,2);# integer #print $y; $z=$y + 1; #print $z; for( 1 .. 10 ) { $start_pt="$x.$y"; # string #print " $start_pt "; $y++; $end_pt="$x.$z"; # string #print " $end_pt "; $z++; $char_read=$t->get("$start_pt","$end_pt"); #print "\nThe charcter:"; #print "\n$char_read" push @chars,$char_read; #print "\nThe word:"; #print "\n@chars"; } #$x=1; #$y=6; #$l=length(@chars); #$fract=(($y+$l)-1); #$wd_end=$x.$fract; #$t->tagConfigure("wrong",-foreground=>"red"); #$t->tagAdd("wrong","$start","$wd_end"); #print "\n@chars"; }

    I'm using the above code inside a subroutine, which is called when a button is clicked in a Tk text widget.

    This for loop: for( 1 .. 10 ) {

    needs to read the word immediately next to the **
    (the first time this pattern is found by $t->search)
    but here this loop reads two words.The loop should stop
    reading characters once it encounters a whitespace.
    I've tried putting a do loop outside the for loop like this

    do { $white_space=" "; for( 1 .. 10 ) { ..... stuff inside for loop. ..... } } until ($char_read eq $white_space);

    but this code just hangs when executed(Argh!).

    What is the problem here?

    Each time the outer foreach loop is executed,
    the search function should look for the first
    occurence of ** in the widget window.The word next
    to the **'s needs to be read, and it's font color set to
    red, using a tag,and the two **'s in front of the word
    deleted.

    The next time around the foreach loop the next ocurrence
    of ** should be located, the next word extracted ,it's
    colour changed, the **'s deleted, and so on.I hope this
    works(!).

    e.g. **word **nextword

    In the above example I need the for loop to read
    'word' and stop once a whitespace is encountered.
    The 'nextword' should be taken care of the next time
    the foreach loop runs.

    Of course we can read a word like this too instead
    of char by char:

    $char_read=$t->get("$start_pt","$start_pt wordend");

    But I'm working on text that is in another language(font)
    and the 'wordend' modifier does not work in my case, though
    it works fine with text in an English font(I tried it)
    So all the trouble.
    Any help would be appreciated.
    Thanx.

      You wrote:

      $start=$result+.2; $x = substr($start,0,1);# integer $y = substr($start,2,2);# integer

      It would be better to work in string mode all the time. If you had "1.8" you would get "2" instead of "1.10".

      How about:

      ($x, $y) = split '\.' => $result; $y += 2;
Re: Re: Simple loop question
by perl_seeker (Scribe) on Aug 08, 2003 at 07:31 UTC
    Hi,
    yes that helps.I would have messed up the indices(2
    instead of 1.10 otherwise)

    I still have the prob. of reading the word next to the
    **'s and stopping at a whitespace, though.
    :)