in reply to Re: What?? delete chars.
in thread What?? delete chars.

Ok, ive rewritten the code to do what I wanted it to do but....
It doesn't work. It prints out the starting text and then stops. Help please. strict says
"my" variable %action masks earlier declaration in same scope at test. +pl line 47 . Useless use of private variable in void context at test.pl line 40. Bareword "LOC" not allowed while "strict subs" in use at test.pl line +95. Bareword "LOC" not allowed while "strict subs" in use at test.pl line +97. Bareword "LOC" not allowed while "strict subs" in use at test.pl line +99.
None of which should stop the program with strict off.
I just dunno. Heres the rewriten code.
use strict; use warnings; print "Welcome.\n"; print "This game will lead you through many perils \n" . "and dangers +that could lead to your death.\n"; print "ONLY THE STRONG WILL SURVIVE\n\n"; my $data; my $action; my @items= (); my $location = "hall"; my $health; my @acdata; my %action; while(<1>){ $data = &data($location); print $data; print "\n\n"; print "What do you want to do?? \n"; print "(type h for help.)\n"; print "Do what??\\: "; my $action = <>; my $noun = &action($action, $location); print $noun; } sub actions{ my $data; my @actdata; my $location; my $action; $data, $location = @_; chomp($data); $data =~ tr/A-Z/a-z/; @acdata = split(/ /, $data); exists $action{$acdata[0]} and $action{$acdata[0]}->(); my %action = ( go => \&go($location, $data) , use => \&use($location, $data) , pickup => \&pickup($location, $data) , drop => \&drop($location, $data) , throw => \&throw($location, $data) , push => \&push($location, $data) , pull => \&pull($location, $data) , search => \&search($location, $data) , check => \&check($location, $data) ); return %action; } sub go{ my ($location, $data) = @_; $data = &chomp_data($data); my $locationdata = &locationdata("go", $location, $data); print $locationdata; return "ok"; } sub chomp_data{ my $data = @_; chomp($data); $data =~ tr/A-Z/a-z/; my @data = split(/ /, $data); shift(@data); return @data; } sub locationdata{ my ($action, $location, $data) = @_; open('LOC', $location) or die "Could not open area script $location.\n +" . "Please check if the file was deleted or +moved.\n"; while(<$location>){ my $start = index(LOC, $action); $start = $start++; my $finish = index(LOC, '>', $start); my $total = $finish - $start - 1; my $cmd = substr(LOC, $start, $total); my @cmdl = split(/:/, $cmd); my @all = split(/#/, @cmdl); my @a = ($all[0], $all[2]); my @b = ($all[0], $all[1]); my @c = ($all[2], $all[3]); if ($cmd eq " "){ my $noun = "You can't do that.($action, $data)"; return $noun; } else{ last; } } } sub data{ my $location = @_; open('LOC', $location) or die "Could not open area script $location.\n +" . "Please check if the file was deleted or +moved.\n"; while(<$location>){ my $info = $_; chomp($info); my $length = length($info); $length = $length - 2; my $data = substr $info, 1, $length; return $data; last; } }

All the Best, Eoin...

If everything seems to be going well, you obviously don't know what the hell is going on.

Replies are listed 'Best First'.
Re: Re: Re: What?? delete chars.
by pfaut (Priest) on Jan 14, 2003 at 19:42 UTC
    "my" variable %action masks earlier declaration in same scope at test.pl line 47.

    This refers to the declaration and initialization of %action in the action subroutine (why do you have so many things with the same name?). You declared a %action at file scope around line 15. You then declare another one midway through the action subroutine after already having referenced the first on the previous line. You are aware that my creates a new variable, correct? You don't have to use my every time you reference a variable for the first time within a block. You do the same thing with a bunch of other variables, as well.

    Useless use of private variable in void context at test.pl line 40.

    This refers to the line $data, $location = @_;. If you wish to assign to a list, you must specify list syntax. That line should read ($data, $location) = @_;. Notice the parenthesis.

    Bareword "LOC" not allowed while "strict subs" in use at test.pl line +95. Bareword "LOC" not allowed while "strict subs" in use at test.pl line +97. Bareword "LOC" not allowed while "strict subs" in use at test.pl line +99.

    You seem to be confused with file handles. LOC in your open statement should not have quotes around it. I also get the feeling you are trying to pass a file handle to index(). That won't work. index() works on strings, not filehandles. If you want to process the contents of a file, you have to read it into memory first. You do that by putting the file handle in angle brackets and assigning the result to a scalar as in my $loctext = <LOC>;. You would then use $loctext in your calls to index.

    That ought to clean up those syntax errors. Logic problems are another story.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';