in reply to foreach increment problem?

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/) {

Replies are listed 'Best First'.
Re: Re: foreach increment problem?
by Grygonos (Chaplain) on Dec 05, 2003 at 19:41 UTC

    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
Re: Re: foreach increment problem?
by Elijah (Hermit) on Dec 05, 2003 at 19:44 UTC
    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.