Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove the last seven characters of a hash value in Perl?

Tags:

I need to cut off the last seven characters of a string (a string that is stored in a hash). What is the easiest way to do that in perl? Many thanks in advance!

like image 637
Abdel Avatar asked May 10 '09 22:05

Abdel


People also ask

How to remove the last character of a string in Perl?

We can use the chop() method to remove the last character of a string in Perl. This method removes the last character present in a string. It returns the character that is removed from the string, as the return value.

How to remove a specific character from a string in Perl?

Perl chop() function is used to remove the last character of the input string and return the chopped character as an output to the user. Chop function has removed the last character from each element from the list, expression, and $_ if we have not specified any value with chop function in Perl.

What is chop command in Perl?

The chop() function in Perl is used to remove the last character from the input string. Syntax: chop(String) Parameters: String : It is the input string whose last characters are removed. Returns: the last removed character.


2 Answers

With substr():

substr($string, 0, -7); 

I suggest you read the Perldoc page on substr() (which I linked to above) before just copying and pasting this into your code. It does what you asked, but substr() is a very useful and versatile function, and I suggest you understand everything you can use it for (by reading the documentation).

Also, in the future, please consider Googling your question (or, in the case of Perl, looking it up on Perldoc) before asking it here. You can find great resources on things like this without having to ask questions here. Not to put down your question, but it's pretty simple, and I think if you tried, you could find the answer on your own.

like image 120
Chris Lutz Avatar answered Nov 14 '22 03:11

Chris Lutz


To remove the last 7 characters:

substr($str, -7) = ''; 

or the somewhat inelegant

substr($str, -7, length($str), ''); 

To get all but the last 7 characters:

substr($str, 0, -7) 
like image 40
ysth Avatar answered Nov 14 '22 03:11

ysth