Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to remove last character of a line

by suvendra123 (Initiate)
on Mar 23, 2021 at 02:31 UTC ( [id://11130167]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to remove last character of a line
by GrandFather (Saint) on Mar 23, 2021 at 03:10 UTC

    Can you really be this thick and unproductively lazy? Surely it takes more time to get an answer here by the time you have written your question and waited, even if just a few minutes, for an answer?

    Get used to trying things out for yourself. Write some trivial code and play with it to see what makes a difference. It will take less time and you will learn more and become more productive than this constant spamming us with badly presented trivia questions.

    Here's something to play with to get you started:

    use strict; use warnings; my $inLine = ".abc(abc), \n"; print "'$inLine'\n"; chomp $inLine; print "'$inLine'\n"; chomp $inLine; print "'$inLine'\n"; chop $inLine; print "'$inLine'\n";

    Run that code for yourself and work out why it prints whatever it prints, then play with it to see what happens. Ask yourself "Why did he put $inLine in single quotes?". Ask yourself "Why did he put a new line at the end of each print?". Ask yourself "Why did he repeat the chomp/print lines?".

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: How to remove last character of a line
by GrandFather (Saint) on Mar 23, 2021 at 03:00 UTC

    chop

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: How to remove last character of a line
by davido (Cardinal) on Mar 23, 2021 at 16:25 UTC

    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

Re: How to remove last character of a line
by Polyglot (Chaplain) on Mar 23, 2021 at 03:03 UTC

    Given:

    $line = '.abc(abc),';

    You can remove the final character by simply saying:

    chop $line;

    This will remove ANY character, even a newline (\n). But to have a little more precision as to which character you might wish to remove, you could use a substitution:

    $line =~ s/.$//; #remove the last character before the newline $line =~ s/[A-Za-z]$//; #remove an alphabetic character before the ne +wline $line =~ s/;$//; #remove a semicolon at the end of the line $line =~ s/[ ]$//; #remove a single space at the end of the line $line =~ s/\s+$//; #remove one or more whitespace characters at the e +nd of the line

    Blessings,

    ~Polyglot~

Re: How to remove last character of a line
by Anonymous Monk on Mar 23, 2021 at 15:50 UTC

    Note that both chomp() and chop() modify their argument in-place and return the removed character. So if $line contains '.abc(abc),, your example code will print either '' (i.e. the empty string) as written, or ',' if you use chop(). If you want your output to be '.abc(abc)', you will need to print $temp_out $line;.

      Note that both chomp() and chop() modify their argument in-place and return the removed character.

      No, only chop returns the removed character. chomp returns the total number of characters removed.

      Edit: Made first sentence more specific.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11130167]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found