in reply to substring function

If you want clean output text, with proper newlines for your OS at the end of each line, instead of that weird dangling ^M (which tells me that perhaps the file isn't native to that OS, and/or was not properly transferred via ASCII when it was FTP'd?), a simple regular expression (or perhaps even something simpler like chomp or chop) might do the job for you:
s/\s*$/\n/s; # assuming \s will catch your newline s/\s*[\r\n]+$/\n/; # otherwise, this should do it
Or, if you just want to use substr:
substr($_, -2, 2) = undef; # deletes last 2 characters substr($_, -3, 3) = "\n"; # if your string ends in a newline

Replies are listed 'Best First'.
RE: Re: substring function
by Anish (Novice) on Sep 29, 2000 at 22:36 UTC
    I do want to preserve that ^M character and also the new line character. Please give any suggestion how to use substring func.to do just that.
      If you literally want to clear the 2nd character from the end (i.e. byte 49 of 50 bytes), you could do this with substr:
      substr($string, -2, 1) = undef;
      That should neatly cut that character out of the string. My instincts tell me you're doing something "the hard way", though. I really cannot see the value of having a binary ^M sitting out near the end of ASCII text file lines. I would only expect to see that with improperly transferred files between systems that use incompatible newlines. I don't quite understand what you're trying to accomplish, but my gut tells me you're going about this problem (whatever your true problem is) the wrong way.

      Good luck.