in reply to What?? delete chars.

Yo Eoin! Greetz from Philly. I glanced at your home node and figured you're too young to be doing perl homework, so let's look at your task and your approach.

Naturally, what you're doing can be done on a command line (you don't need to write a script for this):

perl -pe 's/^\[//; s/\]$//' < hall
That's it. Check the output of "perldoc perlrun" to see what the "-p" and "-e" args do in explicit detail, but in a nutshell, "-e" says "execute the following arg as a script" (best to put single quotes around the script arg), and "-p" says "treat that script as if it were preceded by:
while (<>) {
and followed by
print; }
In the example given, the script is just a couple of regex substitutions, one to remove an initial open-square bracket and the other to remove a final close-square bracket.

Of course, you could save that script as a file that could be run as an executable program, in which case, it would like like this:

#!/path/to/perl # (usually, on *nix, the path is /usr/bin/perl) while (<>) { s/^\[//; # note that the "[" and s/\]$//; # the "]" need to be preceded by backslash print; }
The idea is that things have been set up in perl so that it's easy and concise to do things with $_ -- no need to save its value to other (named) variables for simple operations like this. You could do your substring approach on $_ as well, more compactly:
while (<>) { chomp; print substr( $_, 1, length() - 2 ), $/; }
In this case, though, you're counting on a "prediction" that all lines in your data start and end with a character you don't want on output. If you really know this is true for your data, that's fine. But many of us encounter data where we cannot trust such predictions...

Replies are listed 'Best First'.
Re: Re: What?? delete chars.
by eoin (Monk) on Jan 14, 2003 at 19:02 UTC
    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.
      "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';