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

Input file: 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 special charact +er (its one byte i.e ^M is one character)I'm using following code but + its not working.Any Suggestion please... Thanks. _______________________________________________________________ #!/usr/bin/perl open (diff, "<dave.00063"); + open (out, ">SAPPDAY.00063.new"); + $count=0; + while (<diff>) { + if (/(^16|^88)/) { + chomp $_ ; + @words=split(/,/, $_) ; + $field_cnt=$#words ; + #print " Field count = $field_cnt\n" ; + $last_length=length($words[$field_cnt]) ; + #print " Field length= $last_length\n" ; + $num1 =$last_length-1; + $num2 =$num- 1; + #print " NUM 1 = [$num1]\n" ; + if ( substr($words[$field_cnt], $num1, 1) eq " " ) { + $part1 = substr($words[$field_cnt], 0, $num2 ) ; + $part2 = substr($words[$field_cnt], $last_length, 1 ) ; + $words[$field_cnt] = $part1 . $part2 ; + print "$words[field_cnt]\n" + } + exit; + } + }

Replies are listed 'Best First'.
Re: substring function
by Fastolfe (Vicar) on Sep 29, 2000 at 20:04 UTC
    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
      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.

Re: substring function
by ar0n (Priest) on Sep 29, 2000 at 19:17 UTC
    Input file: 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 special character (it's one byte i.e ^M is one character) I'm using following code but its not working. Any suggestion please... Thanks.
    open (diff, "SAPPDAY.00063.new"); $count = 0; while () { if (/(^16|^88)/) { chomp $_; @words = split(/,/, $_); $field_cnt = $#words; # print " Field count = $field_cnt\n"; $last_length = length $words[$field_cnt]; # print " Field length= $last_length\n"; $num1 = $last_length-1; $num2 = $num - 1; # print " NUM 1 = [$num1]\n"; if ( substr($words[$field_cnt], $num1, 1) eq " " ) { $part1 = substr($words[$field_cnt], 0, $num2 ); $part2 = substr($words[$field_cnt], $last_length, 1); $words[$field_cnt] = $part1 . $part2; print "$words[$field_cnt]\n"; } exit; } }

    [ar0n, super-hero by night]

      #!/usr/bin/perl -w use strict; open FILE, "SAPPDAY.00063.new" or die "Ack! Can't open: $!\n"; while(<FILE>) { if(/^(?:16|88)/) { s/\s+\r\n$/\r\n/g; print; } } close FILE;

      Get a book.

      update
      d'oh! see Fastolfe's post.

      [ar0n]

Re: substring function
by cianoz (Friar) on Sep 29, 2000 at 18:32 UTC
    use the <code> tag when you post your code!