Perl Program

Embed Size (px)

Citation preview

  • 8/11/2019 Perl Program

    1/8

    1> write a script to find factorial of the given number

    2> write a script to check whether the given number is even or odd?

    puts "enter number\n" gets stdin n

    if ($n%2==0) {puts "the given $n is even"} else {puts "the given $n is odd"}

    3> write a script to swap numbers ?

    set L [list 10 20 30 40 50 60 70 80 90 11]set n [llength $L]set i 0set j [expr $n - 1]puts "before swap = $L"while {$i < $j} {set temp [lindex $L $i]set L [lreplace $L $i $i [lindex $L $j]]set L [lreplace $L $j $j $temp]incr i 1

    incr j -1}puts "after temp = $L"

    4> write a script to find the greatest of three number

    puts "enter the number of a"gets stdin aputs "enter the number of b"gets stdin bputs "enter the number of c"

    gets stdin cif {$a==$b && $b==$c && $c==$a} {puts "all are equal"return}if {$a>$b && $a>$c} {puts "a is bigger and value is $a"} elseif {$b>$a && $b>$c} {puts "b is bigger and value is $b"} else {puts "c is bigger and value is $c"}

    5> write a script to check whether the given number is a prime

    puts "enter the number"gets stdin n

    for {set i 2} {$i < $n} {incr i} {set y [expr $n % $i]if {$y == 0} { break }}if { $i == $n } {puts "$n is prime"} else {

  • 8/11/2019 Perl Program

    2/8

    puts "$n is not prime"}

    6> write a script to check whether the number$ strings is polidrome or not

    puts "enter string or number"gets stdin aset len [ string length $a ]set n [ expr $len/2 ]

    for { set i 0 } { $i < $n } { incr i 1 } {set b [ string index $a $i ]set c [ expr $len - 1 - $i ]set d [ string index $a $c ]

    if {$b != $d} {puts "not a palindrome"exit}}puts "Palindrome"

    7> write a script to generate the fibonacci series ?

    puts "enter the number"gets stdin nputs "0"set a 0set b 1set c 0for {set i 1} {$i < $n} {incr i} {set a $bset b $cset c [expr $b + $a]

    puts $c}

    8> write a script to delete a specified line from a text file?

    set n [llength $argv]if { $n != 2 } {

    puts "I need two inputs: filename and lineNumber"exit 1

    }set n [lindex $argv 1]set fin [open [lindex $argv 0] r]set lines [list]

    while { [gets $fin line] >= 0 } {lappend lines $line

    }close $finset c [llength $lines]if { $n < 0 || $n >= $c } {

    puts "the given line number is not available in the file"exit 1

    }set lines [lreplace $lines $n $n]

  • 8/11/2019 Perl Program

    3/8

    set fout [open [lindex $argv 0] w]foreach line $lines {

    puts $fout $line}close $foutputs "deleted the given line successfully"

    9> write a script to replace a specified line in text file ?

    set n [llength $argv]if { $n != 2 } {

    puts "I need two inputs: filename and lineNumber"exit 1

    }set n [lindex $argv 1]set fin [open [lindex $argv 0] r]set lines [list]while { [gets $fin line] >= 0 } {

    lappend lines $line}close $finset c [llength $lines]

    if { $n < 0 || $n >= $c } {puts "the given line number is not available in the file"exit 1

    }puts "enter the line to replace"gets stdin dataset lines [lreplace $lines $n $n $data]set fout [open [lindex $argv 0] w]foreach line $lines {

    puts $fout $line}close $foutputs "replaced the given line successfully"

    10> write a script to to find the number of lines in a text file

    11> write a script to exact data or ip address in logfile ?

    set n [llength $argv]if { $n != 1 } {

    puts "I need one input filename"

    exit 1}

    set fin [open [lindex $argv 0]]set lines [list]while { [gets $fin line] >= 0 } {

    lappend lines $line}close $fin

  • 8/11/2019 Perl Program

    4/8

    set S [join $lines " "]puts $Sset pat {\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}}#set pat {[a-zA-Z]+}set result [regexp -all -inline $pat $S]set n [llength $result]puts "I got $n items in the give file:"puts $result

    12> how to increment eacl element in a list ?e.g:incrlist {1 2 3} => 2 3 4

    set list1 {1 2 3}set list2 {}foreach i $list1 {lappend list2 [expr {$i+1}] }puts $list2

    13> how to swap 30 & 40 in ip address 192.30.40.1 using tcl script?

    set ip {192.30.40.1}puts "before swap : $ip"set values [split $ip .]set temp [lindex $values 1]set values [lreplace $values 1 1 [lindex $values 2]]set values [lreplace $values 2 2 $temp]set ip [join $values .]puts "after swap : $ip"

    14> set ip address as 10.30.20.1 write a script to replace the 50 and 40?

    set a "10.30.20.1"

    set b [string replace $a 3 4 80]set c [string replace $b 6 7 50]puts $c

    15> how to get the next ip for given ip ex: 10.10.10.1 -> 10.10.10.2 ex 10.10.10.255-> 10.10.11.0

    set ip "10.10.11.255"set list [split $ip "."]set ww [lindex $list 0]set xx [lindex $list 1]set yy [lindex $list 2]set zz [lindex $list 3]

    if {$zz>254} {set zz 0incr yyif {$yy>255} {set yy 0incr xxif {$xx>255} {set xx 0incr ww }

  • 8/11/2019 Perl Program

    5/8

    if {$ww>255} {set ww 0}}} else {incr zz}puts "$ww.$xx.$yy.$zz"

    16> Write a regexp to match an ip address?

    set s "hello 12.34.56.78 and 33.44.55.66 ok fine 192.168.1.3 336.23.23.1"

    set ips [regexp -inline -all "\\d{1,3}\.\\d{1,3}\.\\d{1,3}\.\\d{1,3}" $s]

    puts "ips = $ips"

    17> Write a program to reverse a string?

    puts "enter some string :"

    gets stdin s

    set c [string length $s]

    set i 0;

    set j [expr $c - 1]

    while { $i < $j } {set char [string index $s $i]set s [string replace $s $i $i [string index $s $j]]set s [string replace $s $j $j $char]incr i 1incr j -1

    }

    puts "result = $s"

    18> Find the length of a string without using string lengthcommand

    set str "ciscosanketh"

  • 8/11/2019 Perl Program

    6/8

    set len 0set list1 [ split $str "" ]foreach value $list1 {incr len}puts $len

    19> given ip address is valid or not ?

    puts "enter some IP:"gets stdin ipset values [split $ip .]set n [llength $values]if { $n != 4 } {puts "the give ip $ip is not valid"exit 1

    }set f 1foreach value $values {if { $value < 0 || $value > 255 } {

    set f 0break

    }

    } if { $f == 1 } {puts "the given ip $ip is valid"} else {puts "the given ip $ip is not valid"

    }

    1> What do these signify (. * ? +) in regexp?

    it signifys

    . -> any one charatctor

    ? -> zero or one

    + -> one or more

    * -> zero or more

    ^ -> at the beginning of the string

    $ -> " end "

    2> What is command substitution, variable substitution and backslash substitution?(very common question)

    variable substitution command is []puts "enter numbers"gets stdin x

  • 8/11/2019 Perl Program

    7/8

    gets stdin yset x [expr $x+$y]

    puts "additios is =$x"

    backslash substitution command is \set dollar \$fooset x $dollarputs "$x"

    3> Difference between using curly braces and quotes in TCL?

    Double quotes and curly braces are used to group words together into oneargument. The difference between double quotes and curly braces is that quotesallow substitutions to occur in the group, while curly braces preventsubstitutions. This rule applies to command, variable, and backslash substitutions.

    4> Convert a string into a list.

    set str " 1 2 3 4 5 6 7 8 9 10 " set lst [regexp -all -inline {\S+} $str] puts "$lst"

    5> convert a list into string?

    set L [list hello,how are you where are you]

    set s [join $L { }]

    puts "string = $s"

    6> what is upvar and uplevel ?

    UPVAR is used to create aliases

    UPLEVEL is used to execute the code.

    7> what is tcl?

    Tcl (Tool Command Language) is a very powerful but easy tolearn dynamic programming language, suitable for a very widerange of uses, including web and desktop applications,networking, administration, testing and many more. Opensource and business-friendly, Tcl is a mature yet evolvinglanguage that is truly cross platform(windos,all flavors oflinux,macintosh), easily deployed and highly extensible.

    8> How TCL works??

    ---------------Each input from user is treated as TCL command.After providing the command, tclsh fetch(gets) that commandand try to validate/compile/execute and populate(puts) themessage.

    #tclsh% helloinvalid command name "hello"% set

  • 8/11/2019 Perl Program

    8/8

    wrong # args: should be "set varName ?newValue?"%set say "Hello"Hello

    9> How to run a package in tcl?

    source (or)package require