Midterm exam - sub Count...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Midterm exam 1. #This program finds repeated words in an input file and prints the repeated word and the line number on which it was found $i = 0; while (<STDIN>) { $a[$i]=$_; $i++; $hash {$_}++; } foreach $x(keys (%hash)) { if ($hash{$x} != 1) { print("\n$x --- $hash{$x} Times"); $i = 0; foreach(@a) { if($x eq $a[$i]) { $ii = $i + 1; print("\nat line $ii"); } $i++; } } print("\n"); } 2. #write a variance program
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
use strict; use warnings; my @x = " "; my $n = 100; for(my $i=0;$i<$n;$i++) { my $rand = int(rand(10)+10); $x[$i] = $rand; } sub mean() { my $sum = 0; foreach(@x) { $sum = $sum + $_; } $sum = $sum / $n; return $sum; } my $m = mean(); print("Mean :: $m\n"); sub Var() { my $sum = 0; foreach(@x) { $sum = $sum + (($_-$m)*($_-$m)); } $sum = $sum/($n-1); return $sum; } my $var = Var();
Image of page 2
print("Varience :: $var\n"); sub SD() { my $sd=sqrt($var); return $sd; } my $Sd = SD(); print("Standard Deviation :: $Sd\n"); 3. # Count and Sort command using Command line argument
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: sub Count() { open (f1,"<$ARGV[0]")||die("Please enter valid file name. ...!!!!\n"); print("::: Command Count :::\n"); @a = " "; $i = 0; while (<f1>) { $a[$i]=$_; $i++; $hash {$_}++; } foreach $x(keys (%hash)) { if ($hash{$x} != 1) { print("\n$x --- $hash{$x} Times"); $i = 0; foreach(@a) { if($x eq $a[$i]) { $ii = $i + 1; print("\nat line $ii"); } $i++; } } print("\n"); } close(f1); } sub Sort() { open (f1,"<$ARGV[0]")||die("Please enter valid file name. ...!!!!\n"); @file = <f1>; @sorted = sort { $a cmp $b } @file; print("::: Command Sort :::\n"); print @sorted; close(f1); } #open (f1,"<$ARGV[0]")||die("Please enter valid file name. ...!!!!\n"); $cmd = $ARGV[1]; if($cmd eq "count") { Count(); } elsif($cmd eq "sort") { Sort(); } elsif($cmd eq "both") { Count(); Sort(); } else{print("Please enter valid command. ..!!!\n");}...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern