Chinaunix首页 | 论坛 | 博客
  • 博客访问: 571236
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类:

2008-04-13 22:01:46

仿照《PHP Programming Solutions》一书,纯粹写着玩。
 
Chapter 1 Working with Strings
 
1.1 Controlling String Case
 
Problem
 
You want to force a string value to upper- or lowercase.
 
Solution
 
Use the uc() or lc() functions:
 
 

#!/usr/bin/perl
use strict;
use warnings;

my $test = "This is a test.";

print "Uppercase: { " . uc $test . " }\n";
print "Lowercase: { " . lc $test . " }\n";

>perl -w test.pl
Uppercase: { THIS IS A TEST. }
Lowercase: { this is a test. }
>Exit code: 0

 

Comments

When it comes to altering the case of a string, Perl makes it easy with four built-in functions. Two of them a illustated previously: the uc() function uppercase all the characters in a string, it returns an uppercased version of EXPR, and it is the internal function implementing the \U escape in double-quoted strings. The lc() function lowercases all the characters in a string. it returns a lowercased version of EXPR. This is the internal function implementing the \L escape in double-quoted strings.

For more precise control, consider the ucfirst() function, which capitalizes the first character of a string, and the lcfirst() function, which lowercases the first character of a string.  ucfirst() is the internal function implementing the \u escape in double-quoted strings, lcfirst() is the internal function implementing the \l escape in double-quoted strings. Here is an example:

 

#!/usr/bin/perl

use strict;
use warnings;

my $test = "This is a test.";

print lcfirst uc $test . "\n";
print ucfirst lc $test . "\n";

 

>perl -w test.pl
tHIS IS A TEST.
This is a test.
>Exit code: 0

1.2 Checking for Empty String Values

Problem

You want to check if a string value contains valid characters.

Solution

Use a combination of Perl's defined() and our own trim()functions:

 

#!/usr/bin/perl

use strict;
use warnings;

my $test = "            \n";

print (!defined $test or trim($test) eq '' ? "Empty String." : "Not Empty String.");

sub trim {
    $_[0] =~ s/\s+//;
    return $_[0];
}

Comments

You'll use this often when working with form data, to see if a required form field contains valid data or not. The basic technique is simple: use defined() to verify that the string variable exists, then use our trim() function to trim whitespace from the edges and equate it to an empty string. If the test returns true, it's confirmation that the string cotains no value.

1.3 Removing Characters from the Ends of a String

Problem

You want to remove the first/last n characters from a string.

Solution

Use the substr() function to slice off the required number of characters from the beginning or end of the string.

 

#!/usr/bin/perl

use strict;
use warnings;

my $test = "This is a test";

# remove first 6 characters

my $new = substr $test, 6;
print "$new\n";

#remove last 4 characters

$new = substr $test, 0, -4;
print $new;

1.4 Removing Whitespace from Strings

Problem

You want to remove all or some whitespace form a string, or compress multiple spaces in a string.

Solution

 

#!/usr/bin/perl

use strict;
use warnings;

my $test = " This is         a            test            ";

$test =~ s/^\s+//;
$test =~ s/\s+$//;
$test =~ s/\s+/ /g;

print "{$test}";

 
1.5 Reversing Strings
 
Problem
You want to reverse a string.
 
Solution
Use the reverse operator.
 

#!/usr/bin/perl

use strict;
use warnings;

my $test = 'This is a test.';

$test = reverse $test;
print $test;

1.6 Repeating Strings

Problem

You want to repeat a string n times.

Solution

Use the x operator.

#!/usr/bin/perl

use strict;
use warnings;

my $test = 'This is a test.';

print $test x 3;

1.7 Truncating Strings

Problem

You want to truncate a long string to a particular length, and replace the truncated characters with a custom placeholder-for example, with ellipses.

Solution

Use substr()

#!/usr/bin/perl

use strict;
use warnings;

my $test = 'This is a very very very very very very long string.';

print trunc_str($test) . "\n";
print trunc_str($test, 10, '---') . "\n";

sub trunc_str {
    # arg1 => string

    # arg2 => length, you can set a default length

    # arg3 => placeholder, you can set a default holder.


    my $string = shift;
    my $length = shift || 20;
    my $holder = shift || '...';

    return $string if length $string <= $length;
    return substr($string, 0, $length) . $holder;
}

>perl -w test.pl
This is a very very ...
This is a ---
>Exit code: 0

1.8 Converting Between ASCII Characters and Codes

Problem

You want retrive the ASCII code corresponding to a partivular character, or vice versa.

Solution

Use the ord() function to get the ASCII code for a character, use the chr() function to get the character corresponding to an ASCII code.

#!/usr/bin/perl

use strict;
use warnings;

my $test = 'A';

print ord $test;
print "\n";
print chr ord $test;

1.9 Splitting Strings into Smaller Chunks

Problem

You want to break up a long string into smaller segments, each of a fixed size.

Solution

use regex

#!/usr/bin/perl

use strict;
use warnings;

my $test = 'This is a very very very very long, long long string.';
my $size = 6;

my @result = $test =~ /.{$size}/g;
push @result $' if $';


print "$_\n" for @result;

 

>perl -w test.pl
This i
s a ve
ry ver
y very
 very
long,
long l
ong st

ring.
>Exit code: 0

1.10 Comparing Strings for Similarity

Problem

You want to compare two strings to see if they sound similar.

Solution

Use Text::Metahphone or Text::DoubleMetaphone

#!/usr/bin/perl


use strict;
use warnings;

use Text::DoubleMetaphone qw/double_metaphone/;

if (str_similar('rest', 'reset')) {
    print "They are similar\n";
} else {
    print "They are not similar\n";
}

if (str_similar('fire', 'higher')) {
    print "They are similar\n";
} else {
    print "They are not similar\n";
}

sub str_similar {
    my $str1 = shift;
    my $str2 = shift;

    return double_metaphone($str1) eq double_metaphone($str2);
}

1.11 Parsing Comma-Separated Lists

Problem

You want to extract the individual elements of a comma-separated list.

Solution

Use split()

1.12 Parsing URLs

Problem

You want to extract the protocol, domain name, path, or other significant component of a URL.

Solution

URI::Split 

wait wait wait ...

 

----to be continued----

 

阅读(940) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~