This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

lsearch regexp

Help for those learning Tcl or writing their own scripts.
Post Reply
r
rlgraham
Voice
Posts: 6
Joined: Fri Jan 26, 2007 12:10 pm
Location: Little Rock, AR

lsearch regexp

Post by rlgraham »

I am writing a tcl script and I am not familar with how to use the

lsearch -regexp command to get "all" the segments in a file. Here is a peice of my code:

Code: Select all

if {$filenum > 0} {
 set filename [lindex $filelist 0]
 set fileid [open $filename r]
 set newfile [read $fileid]
 set segments [split $newfile \r]

#Here is where I need to know how to get all of the HDR segments not
# just the first one?
[b] set HDRLoc [lsearch -regexp $segments {^HDR}][/b]
My goal is to strip out all the HDR segments except the first one.

Any suggestions will be greatly appreciated.

Thanks,

Ricci
R. G.
Little Rock, AR
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Tried the -all option? Should make lsearch return a list rather than a single value, which you then should be able to use with a foreach-loop...
NML_375
r
rlgraham
Voice
Posts: 6
Joined: Fri Jan 26, 2007 12:10 pm
Location: Little Rock, AR

lsearch regexpr

Post by rlgraham »

That is what I thought I should use, can you show me the syntax for the line I have in bold?
R. G.
Little Rock, AR
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

something like this I suppose:

Code: Select all

set HDRLocList [lsearch -regexp -all $segments {^HDR}]
set HDRKeep 0
foreach HDRLoc $HDRLocList {
 set HDR [lindex $segments $HDRLoc]
 ...
}
In case you're thinking of using lreplace to remove any (and all) HDR segments, be advised that the first lreplace operation would most likely make the list from lsearch invalid, unless you use a "lazy-delete" style of removal...

IE.

Code: Select all

set segments [lreplace $segments $HDRLoc $HDRLoc]
would make the list of HDR-locations invalid, as it alters the list structure; one possible option might be

Code: Select all

set segments [lreplace $segments $HDRLoc $HDRLoc {}]
which does not remove the actual list-entity, but just clears it, and thus retains the integrity of the list of HDR-locations
NML_375
r
rlgraham
Voice
Posts: 6
Joined: Fri Jan 26, 2007 12:10 pm
Location: Little Rock, AR

lsearch regexpr

Post by rlgraham »

I had tried that and it gives this error:

wrong # args: should be "lsearch ?mode? list pattern"
while executing
"lsearch -regexp -all $segments {^HDR}"

so that is why I was trying to get another idea of the way to use the syntax. Any other ideas?
R. G.
Little Rock, AR
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Could you check which version of tcl you are using? Some features are not available in all versions in use today.
NML_375
r
rlgraham
Voice
Posts: 6
Joined: Fri Jan 26, 2007 12:10 pm
Location: Little Rock, AR

Post by rlgraham »

version 8.0
R. G.
Little Rock, AR
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Unfortunately, 8.0 lacks many features such as the -all argument with lsearch.

I would guess the simplest approach (next to bugging your admin to upgrade tcl to 8.4ish), would be to manually step through the list (foreach) and manually test each list-item with regexp, possibly building a new list based on wether each item pass or fail the regexp-test...

ie:

Code: Select all

set new_segments {}
set HDRKeep 0
foreach item $segments {
 if {[regexp -- {^HDR} $item]} {
  if {$HDRKeep == 0} {
   lappend new_segments $item
   set HDRKeep 1
  }
 } else {
  lappend new_segments $item
 }
}
NML_375
r
rlgraham
Voice
Posts: 6
Joined: Fri Jan 26, 2007 12:10 pm
Location: Little Rock, AR

Post by rlgraham »

I thought in the older version you could use a * instead of the -all and get the same thing, but it gives me the same error. I thought I just had the syntax wrong. Are you familar with the * matching all?
R. G.
Little Rock, AR
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

* is a wildcard used with glob-style matching, not regular expressions. It will match 0 or more characters (including whitespaces and other "special" characters).

Using *-wildcard with glob-style matching in lsearch would mean it would match each list-item against *, which would match the first item in the list, and hence return the list index for that item (obviously 0).

There is no *-argument.
NML_375
r
rlgraham
Voice
Posts: 6
Joined: Fri Jan 26, 2007 12:10 pm
Location: Little Rock, AR

Post by rlgraham »

Then I guess I will go with your suggestion of stepping through the segments to find all my HDR's.

Thanks so much for your time, I really appreciate it.

Ricci
R. G.
Little Rock, AR
Post Reply