Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.tcl

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-26-2012, 03:36 PM
Fritz Wuehler
Guest
 
Posts: n/a
Default Can I use a variable for a regexp or other pattern?

Can I use a variable for a regexp pattern or other pattern? For example

regexp {[a-z]\.txt} $string filename

I want to assign "[a-z]\.txt" to a variable and then do something like

regexp {$regExpression} $string filename

but I can't get it to work. I also had no joy with string match and format
etc. Is there a way to do this? It seems like it would be good practice to
assign patterns to variables instead of hardcoding them. Thank you for the
help.

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 03-26-2012, 03:40 PM
Eugene
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

On 26.03.2012 19:36, Fritz Wuehler wrote:
> Can I use a variable for a regexp pattern or other pattern? For example
>
> regexp {[a-z]\.txt} $string filename
>
> I want to assign "[a-z]\.txt" to a variable and then do something like
>
> regexp {$regExpression} $string filename
>
> but I can't get it to work. I also had no joy with string match and format
> etc. Is there a way to do this? It seems like it would be good practice to
> assign patterns to variables instead of hardcoding them. Thank you for the
> help.
>

Here's the output from my TkCon:

(bin) 49 % set filename blablah.txt
blablah.txt
(bin) 50 % set re {[a-z]\.txt}
[a-z]\.txt
(bin) 51 % regexp $re $filename
1
(bin) 52 %

To make things work in your case just remove brackets from around your
$regExpression variable in regex command
--
Best regards, Eugene.
Reply With Quote
  #3 (permalink)  
Old 03-26-2012, 03:42 PM
Eugene
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

On 26.03.2012 19:40, Eugene wrote:
> On 26.03.2012 19:36, Fritz Wuehler wrote:
>> Can I use a variable for a regexp pattern or other pattern? For example
>>
>> regexp {[a-z]\.txt} $string filename
>>
>> I want to assign "[a-z]\.txt" to a variable and then do something like
>>
>> regexp {$regExpression} $string filename
>>
>> but I can't get it to work. I also had no joy with string match and
>> format
>> etc. Is there a way to do this? It seems like it would be good
>> practice to
>> assign patterns to variables instead of hardcoding them. Thank you for
>> the
>> help.
>>

> Here's the output from my TkCon:
>
> (bin) 49 % set filename blablah.txt
> blablah.txt
> (bin) 50 % set re {[a-z]\.txt}
> [a-z]\.txt
> (bin) 51 % regexp $re $filename
> 1
> (bin) 52 %
>
> To make things work in your case just remove brackets from around your
> $regExpression variable in regex command


Completely forgot to explain the behavior - brackets just prevent your
regExpression variable from evaluation/expanding...

--
Best regards, Eugene.
Reply With Quote
  #4 (permalink)  
Old 03-26-2012, 09:50 PM
Nomen Nescio
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

Eugene <eugene.mindrov@gmail.com> wrote:

> On 26.03.2012 19:40, Eugene wrote:
> > On 26.03.2012 19:36, Fritz Wuehler wrote:
> >> Can I use a variable for a regexp pattern or other pattern? For example
> >>
> >> regexp {[a-z]\.txt} $string filename
> >>
> >> I want to assign "[a-z]\.txt" to a variable and then do something like
> >>
> >> regexp {$regExpression} $string filename
> >>
> >> but I can't get it to work. I also had no joy with string match and
> >> format
> >> etc. Is there a way to do this? It seems like it would be good
> >> practice to
> >> assign patterns to variables instead of hardcoding them. Thank you for
> >> the
> >> help.
> >>

> > Here's the output from my TkCon:
> >
> > (bin) 49 % set filename blablah.txt
> > blablah.txt
> > (bin) 50 % set re {[a-z]\.txt}
> > [a-z]\.txt
> > (bin) 51 % regexp $re $filename
> > 1
> > (bin) 52 %
> >
> > To make things work in your case just remove brackets from around your
> > $regExpression variable in regex command

>
> Completely forgot to explain the behavior - brackets just prevent your
> regExpression variable from evaluation/expanding...


I knew that but I didn't know what to do about it!

Thanks again mate :-)



























Reply With Quote
  #5 (permalink)  
Old 03-27-2012, 02:34 PM
Glenn Jackman
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

At 2012-03-26 05:50PM, "Nomen Nescio" wrote:
> Eugene <eugene.mindrov@gmail.com> wrote:
>
> > On 26.03.2012 19:40, Eugene wrote:
> > > On 26.03.2012 19:36, Fritz Wuehler wrote:
> > >> Can I use a variable for a regexp pattern or other pattern? For example
> > >>
> > >> regexp {[a-z]\.txt} $string filename
> > >>
> > >> I want to assign "[a-z]\.txt" to a variable and then do something like
> > >>
> > >> regexp {$regExpression} $string filename
> > >
> > > (bin) 51 % regexp $re $filename
> > > 1


Take note of how the command syntax appears in the man page:
http://tcl.tk/man/tcl8.5/TclCmd/regexp.htm

regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?

Note there's no quotes or braces around 'exp' -- it can be a string, a
variable reference, the result of a command, anything that is a single
word.

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
Reply With Quote
  #6 (permalink)  
Old 03-27-2012, 06:30 PM
Fritz Wuehler
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

Glenn Jackman <glennj@ncf.ca> wrote:

> At 2012-03-26 05:50PM, "Nomen Nescio" wrote:
> > Eugene <eugene.mindrov@gmail.com> wrote:
> >
> > > On 26.03.2012 19:40, Eugene wrote:
> > > > On 26.03.2012 19:36, Fritz Wuehler wrote:
> > > >> Can I use a variable for a regexp pattern or other pattern? For example
> > > >>
> > > >> regexp {[a-z]\.txt} $string filename
> > > >>
> > > >> I want to assign "[a-z]\.txt" to a variable and then do something like
> > > >>
> > > >> regexp {$regExpression} $string filename
> > > >
> > > > (bin) 51 % regexp $re $filename
> > > > 1

>
> Take note of how the command syntax appears in the man page:
> http://tcl.tk/man/tcl8.5/TclCmd/regexp.htm
>
> regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?
>
> Note there's no quotes or braces around 'exp' -- it can be a string, a
> variable reference, the result of a command, anything that is a single
> word.


I have a few books on Tcl and I don't like any of them. I found using the
Tcl doc is a lot better. If I would have looked at it instead of trying to
extend the examples in the books I would have done better. The books do an
ok job of giving examples but I found they leave out many important details
and just focus on what they want to focus on. The Tcl doc is a lot more
helpful so far. regexp is a good example. The various books give examples and
even lists of atoms etc. but they don't tell you the lists they show are not
all inclusive. There is more to this but they pretend it doesn't exist.
Looking at what they show I found out they didn't discuss many things they
should have covered. I guess this is how people teach these days. They
didn't even refer the reader to the Tcl help pages. If I didn't look at the
web site I wouldn't know they even exist. If there a way to get man pages as
well? I must have whatever man pages come with Tcl but I don't know how to
access them.

Thanks for your comment.

Reply With Quote
  #7 (permalink)  
Old 03-27-2012, 07:53 PM
Gerald W. Lester
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

On 3/27/12 1:30 PM, Fritz Wuehler wrote:
> ... If there a way to get man pages as
> well? I must have whatever man pages come with Tcl but I don't know how to
> access them.
>
> Thanks for your comment.
>


If you are on a *nix box with Tcl install (including Mac OSX), try: man n <cmd>

For example: man n foreach

If you are on a Windows box, pull down the help file from ActiveState.


--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald.Lester@kng-consulting.net |
+------------------------------------------------------------------------+
Reply With Quote
  #8 (permalink)  
Old 03-27-2012, 09:56 PM
Nikola
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

On Mar 27, 3:53*pm, "Gerald W. Lester" <Gerald.Les...@KnG-
Consulting.net> wrote:
> On 3/27/12 1:30 PM, Fritz Wuehler wrote:
>
> > ... *If there a way to get man pages as
> > well? I must have whatever man pages come with Tcl but I don't know howto
> > access them.

>
> > Thanks for your comment.

>
> If you are on a *nix box with Tcl install (including Mac OSX), try: man n<cmd>
>
> For example: man n foreach
>
> If you are on a Windows box, pull down the help file from ActiveState.
>
> --
> +------------------------------------------------------------------------+
> | Gerald W. Lester, President, KNG Consulting LLC * * * * * ** * * * * *|
> | Email: Gerald.Les...@kng-consulting.net * * * * * * * ** * * * * * * *|
> +------------------------------------------------------------------------+


Or if you don't have a command prompt handy, but have internet
connection you can search for:
tcl man some_command

and typically the first hit would be a manual page you are looking
for.
For example "tcl man regexp" in google gives:
www.tcl.tk/man/tcl8.4/TclCmd/regexp.htm

Feel free to change 8.4 into 8.5 or 8.6 if you want to see the newer
version of the manual page.
Reply With Quote
  #9 (permalink)  
Old 03-28-2012, 04:12 AM
Fritz Wuehler
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

Eugene <eugene.mindrov@gmail.com> wrote:

> On 26.03.2012 19:36, Fritz Wuehler wrote:
> > Can I use a variable for a regexp pattern or other pattern? For example
> >
> > regexp {[a-z]\.txt} $string filename
> >
> > I want to assign "[a-z]\.txt" to a variable and then do something like
> >
> > regexp {$regExpression} $string filename
> >
> > but I can't get it to work. I also had no joy with string match and format
> > etc. Is there a way to do this? It seems like it would be good practice to
> > assign patterns to variables instead of hardcoding them. Thank you for the
> > help.
> >

> Here's the output from my TkCon:
>
> (bin) 49 % set filename blablah.txt
> blablah.txt
> (bin) 50 % set re {[a-z]\.txt}
> [a-z]\.txt
> (bin) 51 % regexp $re $filename
> 1
> (bin) 52 %
>
> To make things work in your case just remove brackets from around your
> $regExpression variable in regex command


DOH!!!

Thanks! I thought I knew what the problem was but I didn't. I was setting
the variable to contain the regexp incluing the { }

Reply With Quote
  #10 (permalink)  
Old 03-28-2012, 08:00 AM
Nomen Nescio
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

It works! Thank you.


"Gerald W. Lester" <Gerald.Lester@KnG-Consulting.net> wrote:

> On 3/27/12 1:30 PM, Fritz Wuehler wrote:
> > ... If there a way to get man pages as
> > well? I must have whatever man pages come with Tcl but I don't know how to
> > access them.
> >
> > Thanks for your comment.
> >

>
> If you are on a *nix box with Tcl install (including Mac OSX), try: man n <cmd>
>
> For example: man n foreach
>
> If you are on a Windows box, pull down the help file from ActiveState.
>
>
> --
> +------------------------------------------------------------------------+
> | Gerald W. Lester, President, KNG Consulting LLC |
> | Email: Gerald.Lester@kng-consulting.net |
> +------------------------------------------------------------------------+
















Reply With Quote
  #11 (permalink)  
Old 04-06-2012, 12:26 PM
Tibor Stolz
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

> If you are on a *nix box with Tcl install (including Mac OSX), try: man n<cmd>
>
> For example: man n foreach


I've been using the tcl and tk man pages for ages. The call above does
not work on my systems as it is written here, because the first argument
"n" of the command refers to a section of man pages that does not exist.
I've just tested it across machines running different Linux
distributions I have direct or ssh access to, and I found that the
manpage section naming is distribution-dependent:

On Debian and Ubuntu systems:
$ man n lsearch
No manual entry for lsearch in section n
$ man 3tcl lsearch
(that works)
$ man 3t lsearch
(also works, abbreviated section name)

On a Fedora box:
$ man n lsearch
(that works)
$ man 3tcl lsearch
No manual entry for lsearch in section 3t
$ man 3t lsearch
No manual entry for lsearch in section 3tcl

Sometimes (e.g. for Tk procedures like text or checkbutton) it is
possible to leave out the section name at all, but in the case of "man
lsearch", a documentation on a C routine named "lsearch" is displayed;
you need to avoid that by specifying the Tcl manpages section.

By the way, there is no need to google each time for the HTML versions
of the Tcl/Tk manpages: Go to http://tcl.tk -> "DOCUMENTATION" tab ->
one of the 'Quick links' ("Tcl 8.6 (beta)", "Tcl 8.5", or "Tcl 8.4
Docs") [maybe bookmark the resulting page...], and you will find nice
directories of Tcl and Tk commands that will also help you find out if
there exists a (core only) widget to achieve what you want.

Best regards
Tibor Stolz

Reply With Quote
  #12 (permalink)  
Old 04-08-2012, 04:49 PM
Donal K. Fellows
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

On 06/04/2012 13:26, Tibor Stolz wrote:
> I've been using the tcl and tk man pages for ages. The call above does
> not work on my systems as it is written here, because the first argument
> "n" of the command refers to a section of man pages that does not exist.
> I've just tested it across machines running different Linux
> distributions I have direct or ssh access to, and I found that the
> manpage section naming is distribution-dependent:


Tcl itself specifies section 'n', but some distributions move it around
according to their own policies. We can't really help that. (We think of
section '3' and its derivatives as being for C API manual pages.) What I
can say is that we do try to make sure that we have good HTML builds on
www.tcl.tk/man of our manual pages. I think it's about correct for 8.6,
including having all the right cross-links between everything. From 8.5
onwards, there's examples for most Tcl commands and many Tk ones too
(though for Tk widgets you're best off checking the widget demo, to be
honest).

> By the way, there is no need to google each time for the HTML versions
> of the Tcl/Tk manpages: Go to http://tcl.tk -> "DOCUMENTATION" tab ->
> one of the 'Quick links' ("Tcl 8.6 (beta)", "Tcl 8.5", or "Tcl 8.4
> Docs") [maybe bookmark the resulting page...], and you will find nice
> directories of Tcl and Tk commands that will also help you find out if
> there exists a (core only) widget to achieve what you want.


Ah, you've found them! :-)

Donal.
Reply With Quote
  #13 (permalink)  
Old 04-10-2012, 05:58 PM
Nomen Nescio
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

"Donal K. Fellows" <donal.k.fellows@manchester.ac.uk> wrote:

> On 06/04/2012 13:26, Tibor Stolz wrote:
> > I've been using the tcl and tk man pages for ages. The call above does
> > not work on my systems as it is written here, because the first argument
> > "n" of the command refers to a section of man pages that does not exist.
> > I've just tested it across machines running different Linux
> > distributions I have direct or ssh access to, and I found that the
> > manpage section naming is distribution-dependent:

>
> Tcl itself specifies section 'n', but some distributions move it around
> according to their own policies. We can't really help that. (We think of
> section '3' and its derivatives as being for C API manual pages.)


It works as it should on Slackware. I understand you need your own section
to avoid collisions unless you renamed the man pages like tcl_foreach. Maybe
that would have been better. Also it would help if there was some way to
find all the Tcl commands from the main tcl manpage, because man tclsh is
not very useful. Maybe at the bottom you could include a list of all the tcl
commands? Something similar to that is normally done in other man pages.

> What I can say is that we do try to make sure that we have good HTML
> builds on www.tcl.tk/man of our manual pages. I think it's about correct
> for 8.6, including having all the right cross-links between
> everything. From 8.5 onwards, there's examples for most Tcl commands and
> many Tk ones too (though for Tk widgets you're best off checking the
> widget demo, to be honest).


It is uncool to have to be online to get development doc. I'm ok with the
man pages but I very much appreciate you guys have the html tarball
available. This is the best solution. Would love to have one for tcllib as
well. Thanks alot for Tcl and the doc to all Tcl team.


Reply With Quote
  #14 (permalink)  
Old 04-11-2012, 05:32 AM
Rob
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

Nomen Nescio wrote:


>
> It is uncool to have to be online to get development doc. I'm ok with the
> man pages but I very much appreciate you guys have the html tarball
> available. This is the best solution. Would love to have one for tcllib as
> well. Thanks alot for Tcl and the doc to all Tcl team.


Using the URL below, take a look at the tar balls within the relevant
release folder. The one with html in its name contains full documentation,
including that for tcllib & tklib. The documentation is *not* in the form
of man pages but offers, at least IMHO, a more readable form of
documentation.

http://downloads.activestate.com/ActiveTcl/releases/

HTH

Rob.



Reply With Quote
  #15 (permalink)  
Old 04-12-2012, 07:40 AM
Nomen Nescio
Guest
 
Posts: n/a
Default Re: Can I use a variable for a regexp or other pattern?

Hi Rob,

I mean the tcllib hosted on sourceforge for use by Tcl scripts, not the tcl
library that C code can use to get Tcl functions. Unfortunate choice of
names for tcllib, but there we have it. As far as I know it is not included
in the tarballs you mentioned. As I said I already use and am thankful for
the one that covers Tcl Tk and the Tcl and Tk libraries.

Nomen


Rob <dislexic_wobmat@NOSPAMyahoo.com> wrote:

> Nomen Nescio wrote:
>
>
> >
> > It is uncool to have to be online to get development doc. I'm ok with the
> > man pages but I very much appreciate you guys have the html tarball
> > available. This is the best solution. Would love to have one for tcllib as
> > well. Thanks alot for Tcl and the doc to all Tcl team.

>
> Using the URL below, take a look at the tar balls within the relevant
> release folder. The one with html in its name contains full documentation,
> including that for tcllib & tklib. The documentation is *not* in the form
> of man pages but offers, at least IMHO, a more readable form of
> documentation.
>
> http://downloads.activestate.com/ActiveTcl/releases/
>
> HTH
>
> Rob.
>
>
>











Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 01:03 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.