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

I'm having a hard time understanding text manipulation in perl. I want to strip characters from the end of each line in a text file. What operation can strip a certain character/characters from a line / string?

Replies are listed 'Best First'.
RE: Stripping characters from strings
by Adam (Vicar) on Oct 30, 2000 at 00:02 UTC
    AgentM had pretty much the complete list (except chop). Your question, however, is not really specific enough to do more then point you at these documents. But I suspect, rather... HOPE, that you looked at the library before posting your question and have therefor seen these documents (chop,chomp,tr,s) so you probably wanted an example. Lets see:
    use strict; #Always! my $string = "This is a test string. 123\n"; chomp($string); # removes the "\n" from the end of $string. chomp($string); # no-op. chomp only removes white-space: \t, ' ', \n, +etc... chop($string); # removes the '3' from the end of $string. chop($string); # removes the '2'... get the idea? print $string, $/; # print the string, and a new-line. $string =~ s/a //; # replaces an appearance of 'a ' with '' print $string, $/; # print the string, and a new-line. # should read: "This is test string. 1" $string =~ tr/.//d; # remove all periods. print $string, $/; $string =~ s/.$//; # removes the last char. slower then chop. $string =~ s/..$//; # removes the last two characters. my $n = 6; $string =~ s/.{$n}$//; # removes the last $n characters. print $string, $/;
Re: Stripping characters from strings
by AgentM (Curate) on Oct 29, 2000 at 22:36 UTC
Re: Stripping characters from strings
by TGI (Parson) on Oct 30, 2000 at 10:51 UTC
    Also checkout substr. Setting the second argument to a negative number (-1) and the fourth (optional) argument to '' can achieve the same thing, or you can use it like an lvalue and assign a value to it.
    my $foo="foo"; my $bar=substr($foo, -1, 1, ''); print "Foo: $foo\tBar: $bar\n"; # or substr($foo,-1)=''; print "Foo: $foo\n";
    Results:
    Foo: fo        Bar: o
    Foo: f
    
Re: Stripping characters from strings
by moen (Hermit) on Oct 29, 2000 at 23:15 UTC
    You could do something like
    s/.$//g;
    This means: substitute(s/) any one character(.) counting from the end ($) with nothing (//) and at all possible matches(g).
      Is the g needed in this case? Wouldn't this only match the end of line once?
        With an anchor that isn't within an alteration, "g" is pretty useless. I'm not near a perl with Debugging turned on so I can't tell you if it's a no-op, but I do know it isn't a dangerous thing to have on there, just useless. =)

        Update... And what merlyn said below. (yeesh $*)

        --
        $you = new YOU;
        honk() if $you->love(perl)

RE: Stripping characters from strings
by ashok (Sexton) on Nov 02, 2000 at 23:58 UTC
    #Split a string into characters $_ = "Ashok kumar"; @chars = split(//,$_); @chars = reverse @chars;#@chars will have "ramuk kohsA"
RE: Stripping characters from strings
by ashok (Sexton) on Nov 03, 2000 at 00:00 UTC
    #Split a string into characters
    $_ = "Ashok kumar"; @chars = split(//,$_); @chars = reverse @chars;#@chars will have "ramuk kohsA"