Naturally, what you're doing can be done on a command line (you don't need to write a script for this):
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:perl -pe 's/^\[//; s/\]$//' < hall
and followed bywhile (<>) {
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.print; }
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:
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:#!/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; }
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...while (<>) { chomp; print substr( $_, 1, length() - 2 ), $/; }
In reply to Re: What?? delete chars.
by graff
in thread What?? delete chars.
by eoin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |