Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
70 views

Programs Related To Control Structures 1. If-Else

The document discusses different control structures and functions in Perl including if/else statements, while loops, foreach loops, functions with arguments and return values, and reading from and writing to files. Some key points: 1. It shows examples of if/else, if/elsif/else statements and while loops including using next, continue and redo in loops. 2. It demonstrates how to define and call simple functions, functions that return values, and how local and global variables work in functions. 3. Examples are given of using foreach loops with arrays, and passing arguments to functions. 4. Reading from and writing to files is illustrated, including reading line by line and taking user input to write

Uploaded by

Sahil Mahajan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Programs Related To Control Structures 1. If-Else

The document discusses different control structures and functions in Perl including if/else statements, while loops, foreach loops, functions with arguments and return values, and reading from and writing to files. Some key points: 1. It shows examples of if/else, if/elsif/else statements and while loops including using next, continue and redo in loops. 2. It demonstrates how to define and call simple functions, functions that return values, and how local and global variables work in functions. 3. Examples are given of using foreach loops with arrays, and passing arguments to functions. 4. Reading from and writing to files is illustrated, including reading line by line and taking user input to write

Uploaded by

Sahil Mahajan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programs related to control structures 1.

If-else
use strict; my $answer='yes'; if( $answer eq 'yes' ) { print " the answer is yes\n"; } else { print "the answer is no\n"; }

2. If-elsif-else
$x = 5; if ($x !) { print "" is positi#e inte$er%\n"; } elsif ($x & !) { print "" is a ne$ati#e inte$er%\n"; } else { print "" is equal to 'ero\n"; }

3. While loop( with special statements like redo,next continue


$count = !; while ($count &= () { if ($count == )) { print "*+ip ,our-\n"; next; } . /0123 345 6782350 print $count%"\n"; } continue { $count99; . . if($count==:) { print "stoppin$ the loop imme;iately\n";

. .

re;o; } };

print "<oop ,inishe;-\n";

!. While loop(arra"s
.-=usr=>in=perl print "content?type@ text=html \n\n"; Anames = qw(*te#e Bill 6onnor Bra;ley); $n = !; while ($namesC$nD) { print "$namesC$nD\n"; $n99; }

#. $or each loop


.-=usr=>in=perl print "content?type@ text=html \n\n"; Anames = qw(*te#e Bill 6onnor Bra;ley); .$n = !; foreach$names(Anames) { print "$names\n"; }

Programs related to %ash &aria'les in perl 1. %ash &aria'le


my Eperson = ( name = "Foe"G a$e = HIG phone = "555?JKJK"G a;;ress = "JKH Lain *t%"G city = "6hica$o"G state = "1<"G 'ip = ":!:!J"); print "3he name is $person{name}\n\n"; . to print the sin$le +ey an; its #alue my A+ = +eys(Eperson); .to print all the +eys(>ut not #alues) print "3he unsorte; +eys are@ A+\n\n"; my As+ = sort(A+); . or@ As+ = sort(+eys(Eperson)); .to sort the +eys

print "3he sorte; +eys are@ As+\n\n";

Programs related to function in perl 1. (imple function


su> sayMhello { print "helloG worl;-\n"; } sayMhello(); .By con#entionGfunctions are ;efine; a>o#e the point where first calle; >ut can >e ;efine; after it% .sayMhello(); .su> sayMhello { . print "helloG worl;\n"; .}

2. )eturning &alues in functions


su> testJ { $a = J!; $> = JJ; . return the sum $a 9 $>; } su> testK { Aa = ('testin$'G 'one'G 'two'G 'three'); . return the sort of Aa sort(Aa); } $c = testJ(); print "\$c = $c\n"; A> = testK(); print "\A> = A>\n";

3. *ocal and glo'al &aria'le

su> printMmyMi { my $i = J!; print "in printMmyMi()@ $i\n"; } my $i = K!; print "outsi;e printMmyMi()@ $i\n"; printMmyMi(); print "outsi;e printMmyMi()@ $i\n";

!. +rguments in functions
su> stu;ent{ foreach $names(Anames){ print "the names of stu;ents are@Anames\n"; } } Anames=qw(NGBG6GO); stu;ent($names);

Programs related to file handling in perl 1. )eading from a file ( reading each line from a file and print that data to the terminal
.-usr=>in=perl if (- open (,4G "&test%txt")) { ;ie "6an't open test%txt@ $-"; .open(,4G P&test%txtP) or ;ie "6an't open test%txt@ $-";

} else { while ($line = &,4 ) { print "<ine is@ $line";

} } close (,4);

2. Writing to a file ( taking input from user from the terminal and write that data to file
.- =usr=>in=perl use strict; my $line; open (,4G " output%txt") or ;ie "6an't open output%txt@ $-"; while ($line = &*3O12 ) { print ,4 "Qou entere;@ $line"; } close (,4);

You might also like