| View previous topic :: View next topic |
| Author |
Message |
rlgraham Voice
Joined: 26 Jan 2007 Posts: 6 Location: Little Rock, AR
|
Posted: Fri Jan 26, 2007 12:47 pm Post subject: lsearch regexp |
|
|
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: |
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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Jan 26, 2007 2:31 pm Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
rlgraham Voice
Joined: 26 Jan 2007 Posts: 6 Location: Little Rock, AR
|
Posted: Fri Jan 26, 2007 2:35 pm Post subject: lsearch regexpr |
|
|
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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Jan 26, 2007 2:46 pm Post subject: |
|
|
something like this I suppose:
| Code: | 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: | | 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: | | 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
rlgraham Voice
Joined: 26 Jan 2007 Posts: 6 Location: Little Rock, AR
|
Posted: Fri Jan 26, 2007 2:53 pm Post subject: lsearch regexpr |
|
|
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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Jan 26, 2007 2:55 pm Post subject: |
|
|
Could you check which version of tcl you are using? Some features are not available in all versions in use today. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
rlgraham Voice
Joined: 26 Jan 2007 Posts: 6 Location: Little Rock, AR
|
Posted: Fri Jan 26, 2007 2:57 pm Post subject: |
|
|
version 8.0 _________________ R. G.
Little Rock, AR |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Jan 26, 2007 3:08 pm Post subject: |
|
|
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: |
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
rlgraham Voice
Joined: 26 Jan 2007 Posts: 6 Location: Little Rock, AR
|
Posted: Fri Jan 26, 2007 3:12 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Jan 26, 2007 3:16 pm Post subject: |
|
|
* 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
rlgraham Voice
Joined: 26 Jan 2007 Posts: 6 Location: Little Rock, AR
|
Posted: Fri Jan 26, 2007 3:19 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
|