欲忘树 发表于 2017-5-17 09:52:42

Perl Language(VI) String Handler

Perl Language(VI) String Handler

12. String
12.1 Simple String
my $string = "string";
print length($string);

my $mainstring = "Perl Mongers";
my $substring = "Mongers";
print index($mainstring, $substring);

console output:
5

my $mainstring = "perl training by Taipei perl mongers";
my $substring = "perl";
my $first = index($mainstring, $substring);# find the first perl
print index($mainstring, $substring, $first+1);# find the next one

my $mainstring = "perl training by Taipei perl mongers";
my $substring = "perl";
my $pos;
my $current = -1;
until ($pos == -1) {   
    $pos = index($mainstring, $substring, $current + 1);
    $current = $pos unless ($pos == -1);
}
print $current;

substr
my $string = "substring";
print substr($string, 3);

console output:
string

my $string = "substring";
print substr($string, 3, 3);#begin=3, length=3

my $string = "Taipei Perl Mongers";
print substr($string, -12, 4);#begin from back=12,length=4

my $string = "London Perl Mongers";
substr($string, 0, 6) = "Taipei";# only change the first 6 charactors
print $string;

console output:
Taipei Perl Mongers

my $string = "London Perl Mongers";
substr($string, 0, 6, "New York");# fourth parameter
print $string;

12.2 uc & lc
uc is short for upper case

my $string = "I want to get the uppercased string";
print uc $string;

my $string = "upper case";
print ucfirst $string;# print Upper case

lc is short for lower case

12.3 sprintf
my $num = 21.3;
my $formatted = sprintf "%.2f", $num;# define the format
print $formatted;#print 21.30

12.4 Sorting
$tmp = $a;
$a = $b;
$b = $tmp;

equal to

($a, $b) = ($b, $a);

sub my_sort {
my ($a, $b) = @_;
($a, $b) = ($b, $a) if ($a > $b);
}

my @array = (6, 8, 24, 7, 12, 14);
my @ordered = sort { $a <=> $b } @array;
print join "," , @ordered;

console output: 6,7,8,12,14,24

my @array = (6, 8, 24, 7, 12, 14);
my @ordered = sort { $b <=> $a } @array;
print join "," , @ordered; # 24,14,12,8,7,6

12.5 complex sorting

my @ip = ("140.21.135.218", "140.112.22.49", "140.213.21.4", "140.211.42.8");
my @order = sort ipsort @ip;   # we can use sub here
print "$_\n" for @order;

sub ipsort {
    my ($a1, $a2, $a3, $a4) = $a =~ /(\d+).(\d+).(\d+).(\d+)/;# devide into 4
    my ($b1, $b2, $b3, $b4) = $b =~ /(\d+).(\d+).(\d+).(\d+)/;
    $a1 <=> $b1 or $a2 <=> $b2 or $a3 <=> $b3 or $a4 <=> $b4;
}

console output:
140.21.135.218
140.112.22.49
140.211.42.8
140.213.21.4

13. Modules
13.2 Using modules
print your own module path
print "@INC\n";

console output:
/home/luohua/workspace_home/.metadata/.plugins/org.epic.debug
/home/luohua/work/easyperl
/etc/perl
/usr/local/lib/perl/5.10.1
/usr/local/share/perl/5.10.1
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.10
/usr/share/perl/5.10
/usr/local/lib/site_perl .

use Cwd;
my $dir = cwd();
print $dir;

console output:
/home/luohua/work/easyperl/src

use Cwd qw(abs_path);# only use method abs_path
my $dir = Cwd::abs_path;# add the module name before
print $dir;

13.3 Using CPAN and CPANPLUS
CPAN (Comprehensive Perl Archives Network)
>cpan
cpan>install CPANPLUS

Install something from source codes
>perl Makefile.PL
>make
>make test
>make install

Install something forcely
cpan>force install CPANPLUS

download some module
cpan>get CPANPLUS

getting the information of module
cpan>i CPANPLUS

reloading the index of the install files
cpan>reload index

cpan>quit    # for exit

CPANPLUS
>cpanp

List all the modules need updating
cpanp>o

x for exit

13.5 Using your own module
My perl module Personal.pm:
package Personal;
sub adv {
    my @input = @_;
    my $total;
    $total+=$_ for (@input);
    $adv = $total/scalar(@input);
}

1;

My Perl Code:
#!/usr/bin/perl
use lib "/home/luohua/work/easyperl/src";
use Personal;
my @grades = (67, 73, 57, 44, 82, 79, 67, 88, 95, 70);;
my $adv = Personal::adv(@grades);
print $adv;

14. Reference
14.2 Get reference
my @array = (1...10);
my $ref = \@array;
print $ref;   #ARRAY(0x836df90)
print @$ref;#12345678910

15. Database Management

references:
http://easun.org/perl/perl-toc/
http://perl.apache.org/
页: [1]
查看完整版本: Perl Language(VI) String Handler