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...

In reply to Re: What?? delete chars. by graff
in thread What?? delete chars. by eoin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.