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

I am referring to tags in TK::Text, ROText and the like:

my $list = $mw->Scrolled( 'ROText', -height=>5, -width=>20, ) ->pack(-fill=>'both',-expand=>1);

Somewhere, i have this code to replace a line of text in the list.
my $ip = $list->index("T$j.first") || '1.0'; #preserve insertion p +oint #if insertion point is not found, then use 1.0 as insertion point. $list->delete("T$j.start", "T$j.last"); $list->insert($ip, "next text here");
Unfortunately this $list->index("T$j.first") || '1.0' fails if tag $j does not exists. How do I check for the existence of a tag ($j) in said list?
So far, the only method I know is iterate through tagNames
foreach my $i ($list->tagNames()) { if ($i eq $j) { print "$tag found\n"; last; } }

But is there a better method?

Replies are listed 'Best First'.
Re: checking existence of a tag in a TK::Text?
by kcott (Archbishop) on Apr 29, 2018 at 08:41 UTC

    G'day jsteng,

    If you create all tags once and they're stable, you could create a hash and then use exists:

    my %tag_for = map { $_ => 1 } $text->tagNames; ... later ... if (exists $tag_for{$j}) { ... }

    If your tags are in constant flux (i.e. being added and removed) you could use grep to check for existence:

    if (grep $_ eq $j, $text->tagNames) { ... }

    If you used last in your foreach loop for efficiency reasons, take a look at List::Util::first. You might want to Benchmark though: first only allows the "function BLOCK LIST" format:

    first { $_ eq $j } $text->tagNames

    grep allows that format as well as the "function EXPR, LIST" format (which I've shown above). The BLOCK style is typically slower than the EXPR style (certainly for grep, map, and maybe others).

    By the way, I'm not aware of any built-in method like:

    $text->does_tag_exist($tag_name)

    but perhaps someone else does.

    — Ken

      @Ken,
      I like your hash idea!!!

      It is true, those tags are constantly created and deleted; but only *when* the user deletes or adds line manually, creating hashes should not slow down the app nor inconvenience the user experience.

      thanks!
Re: checking existence of a tag in a TK::Text?
by zentara (Cardinal) on Apr 29, 2018 at 19:42 UTC
    I don't know if this will help your understanding, but check out this example, it allows you to get tags at the pointerx and pointery positions, thereby limiting the tag search. Hope it helps.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = tkinit; my $t = $mw->Scrolled('Text', -scrollbars => 'osoe' )->pack; for(1..100){ $t->tagConfigure( 'data'.$_, -data => $_ x 20, ); } for(1..100){ $t->insert('end', 'Line'."$_\n", ['datarider','data'.$_ ]); } $t->tagBind( 'datarider', '<Enter>', sub { getdata($t) } ); $t->tagBind( 'datarider', '<Leave>', sub { getdata($t) } ); $t->bind( '<Motion>', sub{ getdata($t) } ); MainLoop; sub getdata { my ( $text_widget ) = @_; my $x = $text_widget->pointerx - $text_widget->rootx; my $y = $text_widget->pointery - $text_widget->rooty; #print "$x $y\n"; my $txt_index = $text_widget->index( '@' . $x . ',' . $y ); #warn $txt_index; my ( $line, $char ) = ( $txt_index =~ /^(.+?)\.(.+?)$/ ); my @tags = $text_widget->tagNames($txt_index); print "@tags\n"; foreach my $tag(@tags){ print $text_widget->tagCget($tag,'data'),"\n"; } }

    I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: checking existence of a tag in a TK::Text?
by Anonymous Monk on Apr 29, 2018 at 13:44 UTC
      when $list->index("T$j.first") fails the subroutine that contained it returns without doing anything further.
      Therefore I need to know if it exists first before I fetch the index.

        Did you say that out loud? Try saying that out loud. If a tag doesnt exist do nothing . if index says a tag doesnt exist do nothing. Hmmm. Can you hear what youre saying? Do you remember your previously posted code? Each tag had its own callback closure -- it could only be called if a tag existed... Hmmm.. What? Yeah. What? Ok :)