What do you mean by line? And what is in $line? Your question is poorly specified and imprecise.

Let's say your line is exactly this:

.abc(abc),

If that's the case... no trailing newline, nothing like that, you can remove the last character in one of the following ways:

chop($line); # Best way. Removes the last character. $line =~ s/.\z//s; # Works, but not as simple as 'chop': substi +tution. substr($line, -1,1,''); # Works, but not as simple as 'chop': substr + with empty-string replacement. substr($line,-1,1) = ''; # Works, but not as simple as 'chop': substr + as an lvalue.

Pick one of those. The first one, preferably.

But if your string looks like this:

.abc(abc),\n

Then you don't want to remove the last character, you want to remove the last character before the newline. And you may also want to remove the newline. But we don't know because your question was vague, low effort, and imprecise. Let's say you want to remove the comma before the newline, and leave the newline in place. This will work:

$line =~ s/.$//;

This would remove any trailing character that matches the dot metacharacter, and would leave the newline in place.

If you want to remove both the newline and the trailing comma, this would work:

chomp $line; chop $line;

Or this...

$line =~ s/.\n?$//;

This removes the comma, and the newline if it exists. If it doesn't exist, that's fine too.

If your $line is actually multi-line, there's more work to do, but it's still possible. You just need to do some work to understand your data set.

Are you aware of the perldocs? Are you aware that you can use print statements or the debugger to try things out? Reading the Learning Perl and Intermediate Perl books will take you a few days, and will make you vastly more productive, and could make the difference on whether you're worth keeping on a development team.


Dave


In reply to Re: How to remove last character of a line by davido
in thread How to remove last character of a line by suvendra123

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.