Tuesday, September 7th, 2010
Perl string manipulation

Single quote vs double quote in perl

The double-quoted string will have its escape sequences processed, and the single-quoted string will not.#!/usr/bin/perl use warnings; print '\tThis is a single-quoted string.\n'; print "\tThis is a double-quoted ...

More Perl string manipulation

Perl Right trim function to remove trailing whitespace

sub rtrim($) { my $string = shift; $string =~ s/\s+$//; return $string; }Example: # Create a test stringmy $string = " \t Hello world! ...

Perl Left trim function to remove leading whitespace

sub ltrim($) { my $string = shift; $string =~ s/^\s+//; return $string; }Example: # Create a test stringmy $string = " \t Hello world! ...

Perl trim function to remove whitespace from the start and end of the string

sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }Example: # Create a test stringmy $string = " \t Hello ...

Strip out leading whitespace in perl

The code in the example removes leading whitespace from the text of the here document. The /m modifier lets the ^ ...

Interpolating functions and expressions in perl strings

We can construct more complex templates with scalar variable and function interpolation. Developers generally want a function call or expression to ...

Capitalize each word first character, downcase other characters.

$text = "thIS is a loNG liNE"; $text =~ s/(\w+)/\u\L$1/g; print $text; This Is A Long Line

Convert uppercase string to lowercase in Perl

We can use the lc and uc functions or the \L and \U string escapes to convert between upper and lower ...

Check if a string represents a valid number in Perl

$string = 5; if ($string =~ /^[+-]?\d+$/) { # is a integer } else { # is ...

Prints characters in string by ascending ASCII order

%seen = (); $string = "this is a perl tut"; foreach $byte (split //, $string) { $seen{$byte}++; } print "unique chars are: ...

Split string into characters in perl

We can process a string one character at a time by splitting it using the split function and a null parameter ...

Extract string column with unpack in perl

$a = "perl tutorials"; $b = unpack("x6 A8", $a); # skip 6, read next 8 print $b;Output: utorials

Test empty strings in perl

To test for a empty string simply do the following:if ($mystring eq "") { #string is empty }For a null or undefined value ...

Swap the first and last letters in a string using perl

$str = "take a ham"; (substr($str,0,1), substr($str,-1)) = (substr($str,-1), substr($str,0,1)); print $str;Output: make a hat

Using unpack function in perl

The unpack function gives only read access, but is faster when you have many substrings to extract.# get a 5-byte string, ...

Replace substring with string using perl

$string = "This is what you have"; print $string; substr($string, 5, 2) = "wasn't"; # change "is" to "wasn't"

Get substring from string using perl

$string = "This is what you have"; # +012345678901234567890 Indexing forwards (left ...

Access or modify substring using Perl

If developers want to access or modify just a portion of a string, the substring function in perl can be used. ...

Here docs in perl

''Here documents" are a way to quote a large chunk of text. The text can be interpreted as single-quoted, double-quoted, or ...

Quote operators in perl

We can specify strings in a perl program either with single quotes, double quotes using the quote-like operators q// and qq//, ...

eXTReMe Tracker