Anish has asked for the wisdom of the Perl Monks concerning the following question:

INPUT--------------------------------------------------- 03,323868096,,015,0,,,045,0,,,072,0,,,074,0,,,040,0,,,050,0,,,043,0,,,055,0,,/^M 88,100,2287432,1,0,400,2287432,52,12, ^M 16,275,2287432,0,0041880118XF,,/ ^M 88,REMARK : STANDING INSTR FROM: 323-126065 ADT FLOAT XFER CREDIT ^M 16,469,207805,0,2348128762TC,,/ ^M OUTPUT---------------------------------- 03,323868096,,015,0,,,045,0,,,072,0,,,074,0,,,040,0,,,050,0,,,043,0,,,055,0,,/^M 88,100,2287432,1,0,400,2287432,52,12,^M 16,275,2287432,0,0041880118XF,,/^M 88,REMARK : STANDING INSTR FROM: 323-126065 ADT FLOAT XFER CREDIT^M 16,469,207805,0,2348128762TC,,/^M ------------------------------------------------------------------------- I'm trying to get rid of space before ^M.Where ^M is a one character.I want my output file as shown above with new line character...I wanna use SUBstring funct ion as I used in program below. If someone can help me out with this code..also if anyone has other suggestion please let me know.
#!/usr/bin/perl open (diff, "<SAPPDAY.00063"); open (out, ">SAPPDAY.00063.new"); while (<diff>) { if (/(^16|^88)/) { @words=split(/,/, $_); $field_cnt=$#words ; print " Field count = $field_cnt\n" ; $last_length=length($words[$field_cnt]) ; $num1 =$last_length-1; if ( substr($words[$field_cnt], $num1, 1) eq " " ) { $part1 = substr($words[$field_cnt], 0, -2 ); $part2 = substr($words[$field_cnt], $last_length, 1 + ); $words[$field_cnt] = $part1 . $part2; print out "$words[field_cnt]\n"; } } } ~

Replies are listed 'Best First'.
Re: Substring1
by Fastolfe (Vicar) on Oct 02, 2000 at 22:10 UTC
    For those that don't know, this is a repeat of substring function. See that thread for related discussion.

    And I think my last comment there, RE: RE: Re: substring function, answered it (as far as his need to use substr goes). If that did not help you, please respond and explain why and perhaps we can come up with a slightly different approach. I never saw a reply to my post so I assumed you had gotten it working.

    I still think you're doing something weird here. It's not natural to have binary ^M's in ASCII data like that unless you goofed up the ASCII transfer and/or don't understand how newlines work or how to work with ASCII data on multiple platforms. This is very fishy, but if you want to insist on keeping us in the dark, I hope this solution helps you. I don't think this is the root of your problem, though. That's why I don't like solving it under these constraints.

RE: Substring1
by isotope (Deacon) on Oct 02, 2000 at 21:55 UTC
    How about adding this (untested) to the top of your while loop?
    s/\s+\r/\r/g;
    It should remove all whitespace immediately preceding a carriage return (\r or ^M).

    --isotope