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

Ok, I need to solicit the knowledge of the monestary here. I have banged my head for some time now trying to figure out how I can accomplish this task. I have a Tk gui with a field labled "$t". This field is the main field in the gui and will containe some text loaded b y the user. Now I have a "find" function that loads the entire contents of "$t" into an array and then I want to step through each line of the array and try and match the string being searched for.
my @field = $t->get("1.0", "end"); foreach $_ (@field) { if ($_ =~ "\\b$string\\b") { print "Found $string in $_\n"; print $count,"\n"; $status->insert("end", "Found on line " . $count,"\n"); &go_to($count); last; } $count++; print "Count equals $count\n"; }
Now the problem is when I use the $t->get function it does load the entire contents of field $t into the array but when I try to foreach each line the array is not broken into different lines it seems. I toss some debugg print statements in there as you can see and it appears the value of $_ is the entire contents of the array instead of just the first line in the array. The problem is a problem because I need the array to be broken into each line so I can increment the count so I can return the matched line and what line number it was found on. But as far as the loop is concerned the array has only one object therfore the line count never increments past one (the default). How can I perform this function the way I need to?

Replies are listed 'Best First'.
Re: foreach increment problem?
by Roy Johnson (Monsignor) on Dec 05, 2003 at 18:50 UTC
    If by "field" you mean listbox, my reading of the docs indicate that what you're doing should be correct. However, as a workaround, if there are newlines in the string you're getting back, you can just split on them:
    my @field = split /\n/, $t->get('1.0', 'end');

    The PerlMonk tr/// Advocate
Re: foreach increment problem?
by tcf22 (Priest) on Dec 05, 2003 at 18:52 UTC
    $_ doesn't contain all the elements of the array, the problem is that $t->get("1.0", "end") returns the entire contents as a scalar(at least from what you are saying it appears that way), so $fields[0] contains the entire content of the field.

    I haven't really used Tk much, but perhaps this would work:
    my @field = split(/\n/, $t->get("1.0", "end"));

    - Tom

Re: foreach increment problem?
by Zed_Lopez (Chaplain) on Dec 05, 2003 at 18:55 UTC

    Is this a Tk::Text widget? 'cause the docs say:

    $text->get(index1, ?index2?)
    Return a range of characters from the text. The return value will be all the characters in the text starting with the one whose index is index1 and ending just before the one whose index is index2 (the character at index2 will not be returned). If index2 is omitted then the single character at index1 is returned. If there are no characters in the specified range (e.g. index1 is past the end of the file or index2 is less than or equal to index1) then an empty string is returned. If the specified range contains embedded windows, no information about them is included in the returned string.

    It doesn't say anything about returning an array, so I gotta tell you, no you don't have a "find" function that loads the entire contents of "$t" into an array. You have a "find" function that assigns a scalar to an array such that it becomes the array's first element. You want it broken by lines, you'll have to do it yourself.

    my $text = $t->get("1.0", "end"); foreach my $line (split /\n/, $text) {

    Unrelated error: your regexp needs /'s instead of double-quotes, or an 'm' in front of the opening double-quote (or 'm' in front of numerous other possible delimiters.)

    Unrelated style note: for loops assign to $_ by default if nothing else is specified. People don't usually assign to $_ by hand. And regexps are against $_ by default. So you could use your own named variable as I do above, or you could just:

    for (split /\n/, $text) { if (/\b$string\b/) {

      try this $t->get('1.0','1.0 lineend') This should grab the text up to the lineend.. I know it looks incorrect but it works in one of my tk apps for retrieving a line of text.

      good doc on how to use text widgets Tk Docs @ Cornell


      Grygonos
      Cool, I was already working the new line split method but was hoping there was a better way. If I do the split line method I am going to have to refer to each line as $var[0], etc which is what I was trying to get around. And I also never hand assign $_ either but was just one variant I was workjing with at the time of the copy and paste to here. I usually create a scalaer variable of the same name of the array so I do not get lost later. Anyway thanlx for the input but I guess there is no other way then the way I thought about doing it in the first place.