varunreeves has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks.Please help me with this code.This is my beginner's steps in writing perl and I am trying but in vain.The code does not proceed to the next line after getting the $opt2 = <STDIN>; in the main loop.please point out the mistake.I have not used strict but apart from that I cannot see any problem myself

#!/usr/local/bin/perl5 &main_loop; ########################################################## ## Details of this program: ## The program is divided into the following steps and can be underst +ood as follows: ## 1.The location of files are initialized to scalar variables for ea +sy location of the files themselves wherever needed ## 2.The subroutine init_vector is called with an argument(namely: pl +ant,train,place/person).It will for each of the given argument ## by the user store the weight as a "value"of an array of hash whe +re the key value is the "term"and the index is the serial no of the ## .I document i.e the document.Also the subroutine will be designe +d in such a way that by just changing the fixed weight value in the ## subroutine we can easily implement either of the two proposed we +ighing schemes of part 2. ## 3.The subroutine for calculation of centroids is defined. ## 4.Then there will be subroutines for calculation of full cosine si +milarity which will take the inputs from the two centroid vectors ## and return their similarity values,the differences in their valu +es and hence the sorted array.This is then printed out here ## itself. ## Also printed out in the end is the ratio of correct /total coun +ts. ## ## ## ## ########################################################## ########################################################## ## INIT_INPUT_FILENAMES, ## ## This function specifies the names and locations of ## input files used by the program. ## ## Parameter: $type ("stemmed" or "unstemmed") ## ## If $type == "stemmed", the filenames are initialized ## to the versions stemmed , while ## in the default ("unstemmed") case initializes to files ## containing raw, unstemmed tokens. ## ## ########################################################## sub init_input_filenames{ local($type) = @_; $DIR="."; if ($type eq "stemmed") { $plant ="$DIR/plant.stemmed"; $plant_hist ="$DIR/plant.stemmed.hist"; $plant_titles ="$DIR/plant.titles"; $tank ="$DIR/tank.stemmed"; $tank_hist ="$DIR/tank.stemmed.hist"; $tank_titles ="$DIR/tank.titles"; $perplace ="$DIR/perplace.stemmed"; $perplace_hist ="$DIR/perplace.stemmed.hist"; $perplace_titles ="$DIR/perplace.titles"; $common_words ="$DIR/common_words.stemmed"; } else { $plant ="$DIR/plant.tokenized"; $plant_hist ="$DIR/plant.tokenized.hist"; $plant_titles ="$DIR/plant.titles"; $tank ="$DIR/tank.tokenized"; $tank_hist ="$DIR/tank.tokenized.hist"; $tank_titles ="$DIR/tank.titles"; $perplace ="$DIR/perplace.tokenized"; $perplace_hist ="$DIR/perplace.tokenized.hist"; $perplace_titles ="$DIR/perplace.titles"; $common_words ="$DIR/common_words"; } } sub main_loop{ print "Please enter the choice for using the stemmed(1) or unstemm +ed documents(2)\n enter choice:"; $opt_st = <STDIN>; if ($opt_st !~/[12]/){$opt_st = 1;} chomp($opt_st); if($opt_st == 1){ &init_input_filenames("stemmed"); }else{ &init_input_filenames("tokenized"); } print "Please enter the choice for the parameter for which the sim +ilarity values would be calculated:plant(1),tank(2),person/place(3)\n + enter choice:\n opt1:"; $opt1 = <STDIN>; chomp($opt1); if ($opt1 !~/[123]/){$opt1 = 1;} print $opt1; print "Please enter a number for the options you want to have for +weighing : normal(1),eponential decay(2),stepped (3)\n"; print "enter choice for opt2:"; $opt2 = <STDIN>; print"I am a aperl monk"; chomp($opt2); if($opt2 !~ /[123]/){$opt2 = 1;} print $opt2;

Replies are listed 'Best First'.
Re: Help with a perl code
by GrandFather (Saint) on May 03, 2009 at 22:37 UTC
    I have not used strict

    Why not? You obviously know about strictures so why are you not using them (see The strictures, according to Seuss)?

    In general don't use local. It doesn't do what you think it does. Use my instead.

    $opt2 !~ /[123] probably doesn't do what you think it does. It will match any string containing either 1, 2 or 3 as part of the string. It would match 971238 for example.

    Built in functions such as chomp generally are used without parentheses: chomp $value or chomp @values.

    I'd change init_input_filenames to:

    sub init_input_filenames { my ($type) = @_; my $dir = "."; my %fileNames; $type ||= 'tokenized'; $fileNames{plant} ="$dir/plant.$type"; $fileNames{plant_hist} ="$dir/plant.$type.hist"; $fileNames{plant_titles} ="$dir/plant.titles"; $fileNames{tank} ="$dir/tank.$type"; $fileNames{tank_hist} ="$dir/tank.$type.hist"; $fileNames{tank_titles} ="$dir/tank.titles"; $fileNames{perplace} ="$dir/perplace.$type"; $fileNames{perplace_hist} ="$dir/perplace.$type.hist"; $fileNames{perplace_titles} ="$dir/perplace.titles"; $fileNames{common_words} ="$dir/common_words.$type"; return \%fileNames; }

    which halves the code and removes a pile of global variables. The down side is that strict checking doesn't catch misspelled keys for the fileNames hash, but then, without strictures you weren't checking those anyway! At least by using the hash technique you make it clearer where the names are used and where they come from.


    True laziness is hard work
Re: Help with a perl code
by ELISHEVA (Prior) on May 03, 2009 at 12:55 UTC

    On my computer, if I enter 1 for $opt2, I get the print out I am aperl monk1. The "1" at the end comes from print $opt2.

    Maybe you forgot to put in a new line after "I am a Perl Monk" and so couldn't see that you were in fact printing out $opt2 ? The Perl print command does not automatically add a new line at the end of a line. If you wish to add a new line every time you print you can set the perl variable $\ to "\n". See perlvar for more information.

    Best, beth

    update pointed out the "1" being contributed by print $opt2.

      Do u get the print for the line ----- print $opt2; ?? that is the last line in the code