Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I place a colon into a string two characters from the end using Perl?

Tags:

string

perl

I'm trying to find a way to place a colon ( : ) into a string, two characters from the end of the string.

Examples of $meetdays:
1200 => 12:00
900 => 9:00
1340 =>13:40

Not sure if this should be a regular expression or just another function that I'm not aware of.

like image 792
CheeseConQueso Avatar asked Feb 02 '09 21:02

CheeseConQueso


People also ask

How do I split a string into a character in Perl?

If you need to split a string into characters, you can do this: @array = split(//); After this statement executes, @array will be an array of characters. split recognizes the empty pattern as a request to make every character into a separate array element.

How do I split a string into multiple strings in Perl?

Below is the syntax of split function in perl are as follows. Split; (Split function is used to split a string.) Split (Split function is used to split a string into substring) /pattern/ (Pattern is used to split string into substring.)

How do I split a string with multiple delimiters in Perl?

A string is splitted based on delimiter specified by pattern. By default, it whitespace is assumed as delimiter. split syntax is: Split /pattern/, variableName.

What does =~ do in Perl?

The operator =~ associates the string with the regex match and produces a true value if the regex matched, or false if the regex did not match. In our case, World matches the second word in "Hello World" , so the expression is true.


1 Answers

Can also use substr() as well.....

my $string = "1200";
substr $string, -2, 0, ':';

# $string => '12:00';
like image 185
draegtun Avatar answered Sep 24 '22 17:09

draegtun