Re: understanding arrays, and their use
- From: "Roy Terry" <royterry@xxxxxxxxxxxxx>
- Date: Thu, 23 Jun 2005 22:27:59 GMT
"Mr.B" <mark.r.bahlke@xxxxxxxxx> wrote in message
news:1119542624.685437.71510@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> I am having a terrible time trying to comprehend arrays. I have a
> simple task that I would like to do - read in a file, store the
> information in an array, and be able to extract information from
> specific fields in the array as I need the data. I have a pile of
> books that all tell me the same thing about arrays - and I can't
> understand a bit of it. Somebody - please take pity on me, and give me
> a clue - Please!
> Here's my file:
>
> toola option1 string "path/to/resource"
> toola option2 boolean nil
> toola option3 string "itsok"
> toola option4 boolean t
> toola option5 boolean t
> toola option6 string "startup option this is the tricky one"
> toolb option1 string "path/to/another/resource"
> toolb option2 boolean nil
> toolb option3 boolean t
> toolb option4 string "startup now"
> ...
>
> There are several dozen lines in my real data, but it shouldn't make
> much difference. There are extra spaces between the fields - I want to
> treat more than one space the same as one space. Basically there are 4
> fields. The fourth field can be a string enclosed in quotes with
> either a file path, or multiple words separated by spaces. I need to
> treat the 4th group as one field. I want to pull everything "toola"
> out of the file, into an array. Later, I want to go through the array,
> and set a variable named for each option (option1, option2, etc) to the
> value in field 4.
> I have tried lists, scanning, arrays, and I can't make sense of it. So
> far, I have this code:
Issues I see are:
1) use of split can't work because there are
extra spaces and because one field is defined by
quotes. Split has no clue about quotes.
2) You are storing all results in the same
array slot and so are left only with the last
value of $second that was processed. Apparently
you go the notion somewhere that set array(xx) append to
the existing entry - they do not.
3) I see you said in a nearby message that the data is
not well behaved. Question is what about the format *is*
known? I will assume only that if a line starts with the
word you're looking for then it looks something like your
example.
A hybrid approach is needed because only the Tcl command
line and the list-creating commands can properly parse "..." in Tcl.
Here's a re-write
set tolookfor "toola"
set inFile [open $envFile r]
set envArray [split [read $inFile] "\n"]
set w0 ""
foreach line $envArray {
# safely test the first white-space separated word
scan $line %s w0
if { ![string equal $w0 $tolookfor]} continue
# safely find 2 more words and an index value
scan $line %s%s%s%n first second third imore
# grab remainder of line and trim any quotes/spaces on left/right
# store it under key $second
set lookup($second) [string trim [string range $line $imore end] { "}]
}
parray lookup
close $inFile
puts "item $tolookfor = $lookup($tolookfor)"
If you need fancy double quote parsing (escapes and
whatnot) you'll need to
to use the Tcl list parser (with advice to "catch" errors) or
write/find some fancy state-driven code
HTH,
Roy
>
> set tolookfor "toola"
> set inFile [open $envFile r]
> set envArray [split [read $inFile] "\n"]
> foreach line $envArray {
> foreach {first second third fourth } [split $line] { break }
> if {[string compare $tolookfor $first] == 0 } {
> set lookup($first) [list $second]
> puts [array get lookup]
> }
> }
> puts "item $tolookfor = $lookup($tolookfor)"
>
> So this goes through, and scans the file, but what I am left with (aas
> far as I can tell) is only the value from the last line scanned in
> $lookup
> HELP!! I need some answers, or a clue-by-four.
>
> Grasshopper
>
.
- References:
- understanding arrays, and their use
- From: Mr.B
- understanding arrays, and their use
- Prev by Date: Re: Concating strings
- Next by Date: Re: understanding arrays, and their use
- Previous by thread: Re: understanding arrays, and their use
- Next by thread: Re: understanding arrays, and their use
- Index(es):
Relevant Pages
|