So I have a script that replaces certain strings with other strings identified with a regex and a hash. I have the following problem: The way I have written the script, I break lines into substrings, do the substitution on one part but not the other, and then put them together. this works fine. The problem is, the last string in all these files is on character long... so short that when I try to cut in into substrings, there is a failure. It says:
substr outside of string at ReturnA/ReturnAReplacer.pl line 90, <IN> l +ine 8453.

the thing is, I don't care. I realize the last one is always too short, but I want it to go on and deal with the next file anyway. I can't tell it to just ignor the 8574th line because in the next file it might be the 19756th line and so one... forty some-odd files, each with a different number of lines.

Here's the code:

foreach $file (@infile){ open(IN, '<', "$file") or warn $! and next; open(OUT, '>', $outpath.basename($file)) or warn $! and next; while (<IN>) { substr($_,279) =~ s/$regex/$substitute{$1}/g; print OUT $_; } }

In reply to substring too short and I don't care by wrml

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.