Sponsored Content
Full Discussion: Array Operations in tcl
Top Forums UNIX for Advanced & Expert Users Array Operations in tcl Post 302531109 by sarbjit on Thursday 16th of June 2011 02:13:39 AM
Old 06-16-2011
I have implemented using id as key instead of name as two students can have same name but will always have unique names. To seed the students data, i use csv file with id,name,sub1marks, sub2marks, sub3marks as input.

Code:
#!/bin/tclsh
set file_name [lindex $argv 0]
set file_han1 [open $file_name "r"]
while {[gets $file_han1 file_list] > 0} {
    set file_list_split [split $file_list ,]
    set id [lindex $file_list_split 0]
    #Create an array corrs to students name
    set name($id) [string tolower [lindex $file_list_split 1]]
    #Create an array corrs to marks 
    set marks($id) "[lindex $file_list_split 2] [lindex $file_list_split 3] [lindex $file_list_split 4]"   
}

close $file_han1
puts [array get marks]

proc add_func list_value {
    set total_marks 0
    set list_high_value [llength $list_value]
    for {set i 0} {$i < $list_high_value} {incr i} {
    set total_marks [expr $total_marks + [lindex $list_value $i]]
    }
return $total_marks
}

proc position id_st {
    global marks
    foreach id_s [array names marks] {
    set total_marks($id_s) [add_func $marks($id_s)]
}
set memberlist [array get total_marks]
foreach {key value} $memberlist {
        lappend unsortedlist [list $key $value]
}
set sortedlist [lsort -decreasing -index 1 $unsortedlist]
    
foreach pair1 $sortedlist {
lappend total_marks_sorted_lis [lindex $pair1 0] [lindex $pair1 1]
    }
set pos [lsearch $total_marks_sorted_lis $id_st]
    puts "Position is [expr (($pos + 1) + 1) / 2]"
}

puts  "Enter the student id to find its position:"
set id_name [gets stdin]
position $id_name

Enjoy !!
Smilie
This User Gave Thanks to sarbjit For This Post:
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File operations

Hi there, I want some help on scripting regarding file processing. I have a scenario in which I have 10 files. (file1.txt, file2.txt....) and they are in paricular format. I want to read these files and append some text lines at the begining of each file and write this updated contents of... (2 Replies)
Discussion started by: chiragmistry21
2 Replies

2. Shell Programming and Scripting

String Operations

Hi All, Query 1 : I want to know how we can get a count of multipe occurrences of a particular expression in another string. For Eg. If my string is " 12" and i need to count the number of spaces preceeding 12 Query 2 : Also want to know how we can change the alignment of a... (9 Replies)
Discussion started by: Rohini Vijay
9 Replies

3. UNIX for Dummies Questions & Answers

File operations

Hi I have a tab delimited file with 3 fields. I need to sort this file on the first field and remove all the records where the first field has dulplicates. For eg my file is 133|arrfdfdg|sdfdsg 234|asfsdgfs|aasdfs 133|affbfsde|dgfg When this file gets sorted I need the result to be ... (2 Replies)
Discussion started by: monks
2 Replies

4. Shell Programming and Scripting

Help With Array Operations in AWK

I have two files file1 A 2 4 6 8 B 1 3 5 7 C 1 3 5 7 D 1 3 5 7 E 1 3 5 7 file2 C D E F G H I J K L I need to add field 1 of file1 to both field 2s of file2 and repeat the same on all the rows in... (2 Replies)
Discussion started by: cold_Que
2 Replies

5. Shell Programming and Scripting

String operations

Hi All, can you tell me how to drop all preceding zeros in a number. For example, if i have a numbers like 000876838347 and 0000007854762543..how to make them as 876838347 and 7854762543. (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

6. Shell Programming and Scripting

String operations

Can you give me some suggestions to split below string into three parts using shell scripts.. Script has to print all alphabets before the number, then number and then all alphabets after the number.. input: chris martin 200173 845747 mech engineer output: chris martin 200173 845747 mech... (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

7. Linux

Atomic Operations

Hello I am a newbie in linux. Please tell me what are atomic operations in Linux. IS i++ a atomic oparation?? Please help.. (3 Replies)
Discussion started by: aditya08
3 Replies

8. Shell Programming and Scripting

Shell operations from C++

Hi everyone, I need little help in shell operations from C++ program. Here I furnish the details of problem: 1. Lets say my current working path is myWorkingPath. 2. In my working path I have list of name directories and each name directory has two more sub directories say A/B. (now path to... (5 Replies)
Discussion started by: linuxUser_
5 Replies

9. Shell Programming and Scripting

File Operations

Hi Folks, Below is example of an Input data which is used, based on the last 2, 3 & 4 column, I want my first column data to be collated as shown in the output section. a,ac,tc,ic b,ac,tc,ic c,ac,tc,ic d,ac,tc,ic b,bc,tc,ic d,bc,tc,ic e,bc,tc,ic I want my output to be ... (2 Replies)
Discussion started by: nikhil jain
2 Replies
lrange(n)						       Tcl Built-In Commands							 lrange(n)

__________________________________________________________________________________________________________________________________________________

NAME
lrange - Return one or more adjacent elements from a list SYNOPSIS
lrange list first last _________________________________________________________________ DESCRIPTION
List must be a valid Tcl list. This command will return a new list consisting of elements first through last, inclusive. The index values | first and last are interpreted the same as index values for the command string index, supporting simple index arithmetic and indices rela- | tive to the end of the list. If first is less than zero, it is treated as if it were zero. If last is greater than or equal to the number of elements in the list, then it is treated as if it were end. If first is greater than last then an empty string is returned. Note: "lrange list first first" does not always produce the same result as "lindex list first" (although it often does for simple fields that are not enclosed in braces); it does, however, produce exactly the same results as "list [lindex list first]" EXAMPLES
Selecting the first two elements: % lrange {a b c d e} 0 1 a b Selecting the last three elements: % lrange {a b c d e} end-2 end c d e Selecting everything except the first and last element: % lrange {a b c d e} 1 end-1 b c d Selecting a single element with lrange is not the same as doing so with lindex: % set var {some {elements to} select} some {elements to} select % lindex $var 1 elements to % lrange $var 1 1 {elements to} SEE ALSO
list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n), lset(n), lreplace(n), lsort(n), string(n) | KEYWORDS
element, list, range, sublist Tcl 7.4 lrange(n)
All times are GMT -4. The time now is 02:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy