You are on page 1of 5

No.

#!/usr/bin/tclsh

# Procedure to read file content and store it in the list


proc read_file {file_name} {
# Open the file in read mode to read the data
set file_handler [open $file_name "r"]
set data [gets $file_handler]

# Return the contents of the file


return $data
}

# Proc to calculate the sum of all even numbers


proc sum_of_even_nums {data_list} {
# Variable to hold the sum of even numbers
set even_sum 0

# Extraxt each index data


foreach num $data_list {
# Check for even, if even add them
if {$num % 2 eq 0} {
incr even_sum $num
}
}

# Return the sum


return $even_sum
}

# Call the proc to read data


set file "Question_1_file.txt"

# No.1 Read the file & extract the integers


set data [split [read_file $file] ","]
puts $data

# No.2 Sum of all even numbers in the file


set sum [sum_of_even_nums $data]
puts "Sum of the even numbers is: $sum"

# Check if the file containing the data is present or not


if {[open $file "r"] eq -1} {
puts "File not available"
} else {
puts "File available"
}
Output:

No.2
#!/usr/bin/tclsh

if {[catch {[open "text.txt" "r"]} errMsg]} {


puts "Error: $errMsg"
}

set handler [open "texts.txt" "r"]

while {[gets $handler line] ne -1} {


puts $line
}
Output:

F
No.3
#!/usr/bin/tclsh

# Procedure to find the largest number


proc find_large {data} {
# Empty var to store max number
set max 0

# Iterate the list to find the max number


foreach num $data {
if {$num > $max} {
set max $num
}
}

# Return the max number


return $max
}

# List with elements


set data_list {12 42 24 32 76 68 2 4 6 8 9 99}

# Increment each number


foreach num $data_list {
incr num
lappend $data_list $num
}

set data_list [lappend $data_list]

# Call the proc and print the result


puts "The max number is: [set max [find_large $data_list]]"
Output:

.
No.9
#!/usr/bin/tclsh

# Swapping 30 & 40 in 192.30.40.1


# Variable to store the ip address
set ip "192.30.40.1"

# Print the original IP


puts "Original IP: $ip"

# Split the ip and store it in list


set ip_list [split $ip "."]

# Swape the numbers


set ip_list [lappend $ip_list [lindex $ip_list 0] [lindex $ip_list 2] [lindex $ip_list 1]
[lindex $ip_list 3]]

# Print the IP address


puts "After swapping the IP is: [join $ip_list "."]"
Output:

No.10:
#!/usr/bin/tclsh

# Open the file and write 10 lines of content


set file_name "input.txt"

# Opening the file in a+ mode will create the file if it is not there so i can write 10 lines
to it
proc open_write {file_name} {
set file_handler [open $file_name "r"]

# Output file to copy the line containing "tcl"


set keyword "tcl"

# Open the output file in write mode


set output [open "output.txt" "w"]
while {[gets $file_handler line] ne -1} {
if {[string match *$keyword* $line]} {
puts $output $line
}
}

# Close the file


close $file_handler
close $output
}

if {[catch {[open $file_name "r"]} errMsg]} {


puts "Error: $errMsg"
}

# Call the proc to do the operation


open_write $file_name
Output:

You might also like