Perl is popular. It is good for what it was intended, and it is freely available (and available for free). #!/public/bin/perl while(<STDIN>) { chop; foreach (split(/ /)) { if (defined($aa{$_})) { $aa{$_}++; } else { $aa{$_} = 1; } } } foreach (sort(keys(%aa))) { print "$_: $aa{$_}\n"; } (and: 1 It: 1 Perl: 1 and: 1 available: 2 for: 2 free).: 1 freely: 1 good: 1 intended,: 1 is: 3 it: 2 popular.: 1 was: 1 what: 12) Modify the program from question 1 so that it is no longer case sensitive (interprets all input as lower case) and ignores non-alphanumeric characters.
change the 'foreach (split(/ /)) { line to: 'foreach (split(/W+/)) { and add the line 'tr/a-z/A-Z/ right before the if statement3) What does the following Perl program do? Be as precise as possible. Why is line 4 ($name = s/\*/.*/) necessary?#!/public/bin/perl chop($name = <STDIN>); $name =~ s/\*/.*/; &look($name,"."); sub look { local ($n,$dir) = @_; opendir(CURRENT,$dir); @list = readdir(CURRENT); closedir(CURRENT); foreach (sort @list) { $path = $dir.'/'.$_; if (-d $path) { if (-r $path && $_ ne '.' && $_ ne '..') { &look($n,$path); } } elsif (/^$n$/) { print "$path\n"; } } }
The program reads a string from STDIN. It then looks for all filenames with the same name in the current directory and the entire sub-directory tree, outputing the full path from the current directory to each of the files found. It does this in alphabetical order.
Line 4 is needed so that the input string can contain a * as a wildcard to be used in the unix fashion. Since the program uses a Perl regular expression, this must be converted to the Perl string ".*", which has the same meaning as the Unix "*".