简体   繁体   中英

Tcl file content processing

I have a.csv file with below data

January,A1,100 
January,A2,200
January,A3,300
January,B1,100
January,B2,300
February,A1,100
February,A2,200
February,B2,200
March,C1,200
March,B1,400
April,D1,100

I need to read the above.csv file from disk, process it and display it's summary as below. How do I convert the above raw data into below values?

Month       A     B
January    600   400
February   300   200
March        0   400
April        0     0

The formula for A is total of values in index 2 when index 1 is A1/A2/A3(0 if not available)

The formula for B is total of values in index 2 when index 1 values is B1/B2/B3(0 if not available)

What I tried:

set file [open $loc r]
while {[gets $file sline] >=0} {
  regsub {"(.*?),(.*?)"} $sLine {\1:\2} sLine 
  set lsLine [split $sLine ","]
  set month [lindex $lsLine 0]
  lappend mnList $month
  set monthList [lsort -unique $mnList]
}

I did figure out the formating part with below code.

set Month $monthList 
set A {600 300 0 0}
set B {400 200 400 0}

set formatStr {%15s%15s%15s}
puts [format $formatStr "Month" "A" "B"]
foreach month $Month a $A b $B {
puts [format $formatStr $month $a $b]
}  

I am having hard time calculating the values of A and B columns from the file. I tried multiple ways inside foreach month $monthList but none helped me to get the values I need:(

PS Pretty new to tcl and coding

set file [open file.csv r]
set data {}
while {[gets $file line] != -1} {
    lassign [split $line ,] month key value
    dict update data $month monthData {
        dict incr monthData [string range $key 0 0] $value
    }
}
# $data contains:
# January {A 600 B 400} February {A 300 B 200} March {C 200 B 400} April {D 100}

proc dictGetDef {dictValue key default} {
    if {![dict exists $dictValue $key]} {
        return $default
    }
    return [dict get $dictValue $key]
}

dict for {month monthData} $data {
    puts [format {%-10s %5d %5d} $month [dictGetDef $monthData A 0] [dictGetDef $monthData B 0]]
}
January      600   400
February     300   200
March          0   400
April          0     0

Tcl 8.7 has dict getdef natively, but better.


To restrict the accumulating to a set of specific keys can be done is several ways. Using switch makes it quite explicit:

while {[gets $file line] != -1} {
    lassign [split $line ,] month key value
    switch -- $key {
        A1 - A2 - A3 -
        B1 - B2 - B3 {
            dict update data $month monthData {
                dict incr monthData [string range $key 0 0] $value
            }
        }
    }
}

You give switch pattern-body pairs. If the body is - then it falls through to the next body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM