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

:)Hello,
it seems a bit silly to be asking this,but anyway consider this bit of code:
foreach $mitem(@missing) { #print"\n$mitem"; $result=$t->search(-forwards,"**",'end'); #print"\nThe start pos of the pattern:"; #print "\n$result"; #1.4 ($x, $y) = split '\.' => $result; $y += 2; #print "\n$x"; #print "\n$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"; last if $char_read eq /\s/; } }
Consider the for loop
for( 1 .. 10 )
This loop reads a word at a particular position from a Tk text widget char by char.I need the for loop to stop if $char_read is a white space i.e. at the end of the word. This line in the code is not working,and the loop reads the first word , the whitespaces next to it and some characters of the next word too. last if $char_read eq /\s/; I've tried using the unless and until modifiers after the for loop but I keep getting a syntax error.

Something like

white_space=" "; #for loop stuff until $char_read eq white_space;
I've tried putting a do loop (with an until modifier) around the for loop,this makes the code just hang.

To be noted is that this for loop is inside a foreach loop This thing is really driving me crazy.What is the problem here?Any solutions?

Thanx, :)

Edit by tye, remove rampant BR tags

Replies are listed 'Best First'.
Re: Regex and loop question
by MidLifeXis (Monsignor) on Aug 20, 2003 at 10:23 UTC

    You want don't want to use eq and a pattern. Use something like this instead...

    for (1..10) { ... last if ($charread =~ /\s/); }

    or

    for (1..10) { ... last if ($charread eq " " || $charread eq "\t" ....); }

    Update: Reread the supplied code :)

    Update 2: replace $_ with $charread.

      Hi!
      Your first code bit solved my tiresome whitespace
      problem.Thanks a lot.
      :)
Re: Regex and loop question
by hmerrill (Friar) on Aug 20, 2003 at 13:54 UTC
    MidLifeXis already answered your question with
    last if $char_read =~ /\s/;
    but I thought I would add that since you're not familiar with regular expressions, you might find the excellent 'perldocs' documentation that's included with Perl helpful in learning about regular expressions and pattern matching.

    Do
    perldoc perl
    at a command prompt, and you'll see all the different perldocs that come with perl. Listed there are these regular expression topics:
    perlrequick Perl regular expressions quick start perlretut Perl regular expressions tutorial perlre Perl regular expressions, the rest of t +he story
    So, then to read the perldocs on perlrequick, you would do
    perldoc perlrequick
    Hope this helps.
      Yes, this is also useful.
      Thanx.:)
Re: Regex and loop question
by CombatSquirrel (Hermit) on Aug 20, 2003 at 12:15 UTC
    Forgive me my question, I know about nothing of Tk, but if you already use a RegEx, why don't you let it do all the work for you? Something like this
    #!perl use strict; use warnings; my $string = "I don't know what to do ** when I do this"; if ($string =~ /(.*?\*\*.*?)(\b[a-zA-Z']+\b)/) { print "Match <$2> at offset " . length($1) . $/; } else { print "No match" . $/; } my @chars = (split //, $2); @chars = @chars[0..9] if (@chars > 10); print join(' - ', @chars) . $/; __END__ OUTPUT: Match <when> at offset 27 w - h - e - n
    should in my eyes work if $string contained the contents of the text field.
    But again, I hardly know any Tk, and if there should be any problem with the code above, I'd be happy if you pointed me to it.
      Hi,
      Your code would also work to some extent, I guess. We could put all the text in the Tk window in a scalar
      variable, and do a regex match with the string stored in that variable.

      Your offset value of '27' would give me the character number of the starting point of the word.But I would
      also need the line number if I am to do something further with the word, for example change the color of the word using code like this:
      $t->tagConfigure("wrong",-foreground=>"red"); $t->tagAdd("wrong","$start","$end_pt");
      Tk text widget indices are of the form: line no.char no, chars begin at 0, lines begin at 1

      i.e. I would need => line no.27

      Thanx for the regex bit of code anyway.
      :)