Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

shell-to-array (See related posts)

Demonstration: How to obtain variables / arrays from unix shell

#!/usr/bin/tclsh
# Demonstration: How to obtain variables / arrays from unix shell
#

proc tokenize {buf} {
   set exp {[^ \t]*[ \t]*}
   # use "-indices" if you just care about the indices
   return [regexp -all -inline -- $exp $buf]
}

# get a number from shell
set p [exec ls -1 | grep "" -c ]
puts "Got a number from shell: $p"

set v [exec ls -1]

# the RegExp way: Matches everything from the beginning to the end of a line
set exp {^.*$} 

# create an array of files
set filelist [regexp -line -all -inline -- $exp $v]

# The ancient C-Way would have looked like this:
#for { set i 0} {$i < $p} { incr i } {
#   scan $v "%s%n" file length
#   lappend filelist $file
#   set v [string range $v $length end]
#}
#

# show list
foreach file $filelist  {
   puts $file
}

# and tell me the length
puts [llength $filelist ]


You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts