Re: understanding arrays, and their use




"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
>


.



Relevant Pages

  • understanding arrays, and their use
    ... specific fields in the array as I need the data. ... toola option2 boolean nil ...
    (comp.lang.tcl)
  • Re: understanding arrays, and their use
    ... # specific fields in the array as I need the data. ... # toola option2 boolean nil ...
    (comp.lang.tcl)
  • Re: memory usage of Virtual function poiters
    ... It's not clear to me what your array is going to hold. ... given instance of Option2 can only hold one type. ... pointers to aBaseClass, and when retrieving a pointer from the array you'd ... and not a pointer for each virtual function. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: memory usage of Virtual function poiters
    ... >It is my understanding at this moment in time that with Option2 a VFPT ... But you cannot have arrays of mixed types even if they share a common ... You can have arrays of pointers to a common base of a polymorphic ... problem you are trying to solve with an array of 'mixed types' it is ...
    (alt.comp.lang.learn.c-cpp)