Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

lsearch -regexp difficulty, why won't it work?
Ok,

I'm writing a proc that may or may not get passed a timeout value.  Due
to other code writing practices, I must implement this functionality
using the "args" argument rather than simply saying

proc procName { yadda { timeout val } } { }

construct.

So, in parsing my args list I'm trying to use lsearch to accomplish this
as in the following example:

lsearch -regexp {^[0-9]+$} $lArgs
# lArgs contains args after string tolower

This doesn't work.  In trying to figure out why I've tried the following
loop which also doesn't work:

set lst "arg1 450"
foreach x $lst {
regexp -inline {^[0-9]+$} $x
}

However, when not working with lists but rather doing:

regexp -inline {^[0-9]+$} 4432100933

Works as expected.  So, what gives?  What little TCL idiocyncrosy am I
missing?  Does this have something to do with how the variable is
actually being stored in memory?  Is lsearch presenting the data in a
way that *something* is preventing this regular expression from
matching?  I do have a restriction on me in that I should use the ^ and
$ in the regexp because the possibility of other numbers being passed to
function does exist.



---------------------------------------------
Andrew R. Falanga (a non-HP employee)
Hewlett-Packard Company
11311 Chinden Blvd.
Boise, Idaho
---------------------------------------------
Please note: The e-mail address is purposely
mangled.  I do not wish my account at HP to
become a spam haven.

Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Falanga
04-21-05 09:00 PM


Re: lsearch -regexp difficulty, why won't it work?
Andrew Falanga wrote:
> This doesn't work.  In trying to figure out why I've tried the following
> loop which also doesn't work:
>
> set lst "arg1 450"
> foreach x $lst {
>    regexp -inline {^[0-9]+$} $x
> }

Why do you believe this doesn't work?  It works for me:

% set lst "arg1 450"
arg1 450
% foreach x $lst {
puts [regexp -inline {^[0-9]+$} $x]
}

450
%

--
| Don Porter          Mathematical and Computational Sciences Division |
| donald.porter@nist.gov             Information Technology Laboratory |
| http://math.nist.gov/~DPorter/                                  NIST |
 |_______________________________________
_______________________________|

Report this thread to moderator Post Follow-up to this message
Old Post
Don Porter
04-21-05 09:00 PM


Re: lsearch -regexp difficulty, why won't it work?
Andrew Falanga <not_real@hp.com> writes:

> other code writing practices, I must implement this functionality using th
e
> "args" argument rather than simply saying
>
> proc procName { yadda { timeout val } } { }

Based on this statement I wonder why you are trying to use regexp.
You can get the same effect using:

if {[llength $args]>1} {
set timeout [lindex $args 1]
} else {
set timeout 60
}

but shorter is

set timeout 60
foreach {yadda timeout} $args break

Then you can test $timeout for validity.

Disclaimer: I cannot imagine what "other code writing practices"
apply the constraints.


--
Donald Arseneau                          asnd@triumf.ca

Report this thread to moderator Post Follow-up to this message
Old Post
Donald Arseneau
04-22-05 01:58 AM


Re: lsearch -regexp difficulty, why won't it work?
Andrew Falanga wrote:
>
> Ok,
>
> I'm writing a proc that may or may not get passed a timeout value.  Due
> to other code writing practices, I must implement this functionality
> using the "args" argument rather than simply saying
>
> proc procName { yadda { timeout val } } { }
>
> construct.
>
> So, in parsing my args list I'm trying to use lsearch to accomplish this
> as in the following example:
>
> lsearch -regexp {^[0-9]+$} $lArgs
> # lArgs contains args after string tolower
>
> This doesn't work.  In trying to figure out why I've tried the following
> loop which also doesn't work:
>
> set lst "arg1 450"
> foreach x $lst {
>    regexp -inline {^[0-9]+$} $x
> }
>
> However, when not working with lists but rather doing:
>
> regexp -inline {^[0-9]+$} 4432100933
>
> Works as expected.  So, what gives?  What little TCL idiocyncrosy am I
> missing?  Does this have something to do with how the variable is
> actually being stored in memory?  Is lsearch presenting the data in a
> way that *something* is preventing this regular expression from
> matching?  I do have a restriction on me in that I should use the ^ and
> $ in the regexp because the possibility of other numbers being passed to
> function does exist.
>
>

One problem that I can see: foreach does not return a value. You would
have to use:

set success 0
foreach x $lst {
if { [regexp -inline (^[0-9]+$} $x] } {
set success 1
break
}
}

or something similar.

But there are plenty of other solutions as Donald Arseneau indicates.
Let me add: [string is integer -strict $x] - easier than the regexp.


Regards,

Arjen

Report this thread to moderator Post Follow-up to this message
Old Post
Arjen Markus
04-22-05 01:58 PM


Re: lsearch -regexp difficulty, why won't it work?
On Fri, 22 Apr 2005, Arjen Markus wrote:

> But there are plenty of other solutions as Donald Arseneau indicates.
> Let me add: [string is integer -strict $x] - easier than the regexp.

foreach z {123 -123 0x123 09121233 82956256298562} {
puts "$z is[expr {[string is integer -strict $z] ? {} : { not}}]\
plain numbers."
}

--
-Kaitzschu
s="TCL ";while true;do echo -en "\r$s";s=${s:1:${#s}}${s:0:1};sleep .1;done

Report this thread to moderator Post Follow-up to this message
Old Post
Kaitzschu
04-22-05 01:58 PM


Re: lsearch -regexp difficulty, why won't it work?
According to Andrew Falanga  <not_real@hp.com>:
:lsearch -regexp {^[0-9]+$} $lArgs
:# lArgs contains args after string tolower
:
:This doesn't work.  In trying to figure out why I've tried the following
:loop which also doesn't work:
:
:set lst "arg1 450"
:foreach x $lst {
:   regexp -inline {^[0-9]+$} $x
:}

Try this and see:
# set lst "arg1 450"
arg1 450
% foreach x $lst {
set a [regexp -inline {^[0-9]+$} $x]
puts "a = $a"
}
a =
a = 450
%

--
<URL: http://wiki.tcl.tk/ > MP3 ID tag repair < http://www.fixtunes.com/?C=17038[/<...rg/NET/lvirden/ >

Report this thread to moderator Post Follow-up to this message
Old Post
lvirden@gmail.com
04-22-05 09:01 PM


Re: lsearch -regexp difficulty, why won't it work?
Donald Arseneau wrote:
> Andrew Falanga <not_real@hp.com> writes:
>
> 
>
>
> Based on this statement I wonder why you are trying to use regexp.
> You can get the same effect using:
>
> if {[llength $args]>1} {
>     set timeout [lindex $args 1]
> } else {
>     set timeout 60
> }
>
> but shorter is
>
> set timeout 60
> foreach {yadda timeout} $args break
>
> Then you can test $timeout for validity.
>
> Disclaimer: I cannot imagine what "other code writing practices"
> apply the constraints.
>
>

The coding practices to which I refer require that all procs written use
only the args argument versus spelling out expected arguments.  Further,
the proc I'm modifying already "accepts" one option argument and I'm
adding another.  Thus, I can never guarantee in what order they will
appear.  Thus rendering any use of lindex with hard coded index values
useless.

Thanks to all for the suggestions.

---------------------------------------------
Andrew R. Falanga (a non-HP employee)
Hewlett-Packard Company
11311 Chinden Blvd.
Boise, Idaho
---------------------------------------------
Please note: The e-mail address is purposely
mangled.  I do not wish my account at HP to
become a spam haven.

Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Falanga
04-22-05 09:01 PM


Re: lsearch -regexp difficulty, why won't it work?
Andrew Falanga <not_real@hp.com> writes:

> The coding practices to which I refer require that all procs written use o
nly
> the args argument versus spelling out expected arguments.

... which sounded entirely contrived ...

> Thus, I can never guarantee in what order they will appear.

Ah!  Having no *positional* arguments is a good reason to use
$args.  And what I suggested with foreach *is* for positional
arguments, just like the argument-list with defaults you said
you would have used.

Most of the existing code for random $args handling is directed
towards switches/options. The core uses a proc tclParseConfigSpec.


--
Donald Arseneau                          asnd@triumf.ca

Report this thread to moderator Post Follow-up to this message
Old Post
Donald Arseneau
04-23-05 01:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Tcl archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:24 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.