#!/bin/perl # MISCELLANEOUS # Reading from a file: open DICT, "smallDict.txt"; chomp(@words = ); # Wow! Now that's easy! print ((scalar @words), " words loaded.\n"); # Generating random numbers: $randFloat = rand(); # random number from 0.0 to 0.999999999.. $wordNum = $randFloat * @words; $word = $words[$wordNum]; print "random number $randFloat\n"; print "-> word number $wordNum\n"; print "-> word: \"$word\"\n"; # Misc string operations: $wordLen = length $word; print "... which has $wordLen characters.\n"; $r1 = rand() * $wordLen; $r2 = rand() * $wordLen; if ($r2 < $r1) { $temp = $r1; $r1 = $r2; $r2 = $temp; # swap 'em } $r2 -= $r1; print "Random substring index: $r1\n"; print "Random substring length: $r2\n"; $randSubstr = substr($word, $r1, $r2); print "Random substring \"$randSubstr\" "; $index = index($word, $randSubstr); print "found at index $index of \"$word\".\n"; #You can also specify a starting index for the index search: $ms = "Mississippi"; print "Positions at which \"i\" occurs in $ms:\n"; #$index = 0; while (($index = index($ms, "i", $index)) != -1) { print $index, " "; $index++; } print "\n";