- 浏览: 2556084 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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/
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/
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 479NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 339Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 440Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 390Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 482NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 426Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 340Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 252GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 454GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 330GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 316Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 323Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 297Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 314Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 293NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 264Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 577NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 271Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 379Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 380Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
public void sendMessageToActivity(int what, String data) { Message msg = mHandler.obtainMessage(what); msg.obj = data; mActivityHandler.sendMessage(msg); // 假设mActivityHandler是Activity的Handler...
String result = performBackgroundTask(); // 创建Message并附带数据 Message message = Message.obtain(); Bundle bundle = new Bundle(); bundle.putString("result", result); message.setData(bundle); ...
String data = "从子线程获取的数据"; Message msg = new Message(); msg.what = 1; // 设置标识 msg.obj = data; // 设置数据 mHandler.sendMessage(msg); // 将消息发送到主线程 } }).start(); ``` 3. ...
textView.setText((String) msg.obj); // 更新UI break; default: super.handleMessage(msg); } } ``` 4. **避免内存泄漏**: `Handler`可能会导致内存泄漏,因为它持有对`Activity`的引用。为防止内存泄漏...
在Android开发中,`Handler`是一个至关重要的组件,主要用于解决线程间的通信问题,尤其是在UI线程和其他工作线程之间。本篇文章将深入探讨`Handler`的使用方法,包括其基本概念、工作原理以及如何避免内存泄露和...
在Android开发中,`Handler`是一个至关重要的组件,它用于在主线程中处理来自其他线程的消息,确保UI更新和事件处理的同步性。本文将详细介绍`Handler`的几种常见写法,以及如何使用`Handler.Callback`进行消息处理...
此时,Android提供了多种多线程解决方案,其中之一就是Handler机制。本篇将详细讲解如何在Android中使用Handler进行线程间通信。 Handler是Android中处理消息和回调的核心组件,它与Looper和Message紧密配合,实现...
"JobHandler调度器"是一种广泛应用于分布式任务调度的框架,主要设计用于处理大规模并发和定时任务的场景。在这个系统中,JobHandler是核心组件,它负责执行具体的业务逻辑。下面我们将详细探讨JobHandler调度器的...
如果需要在不同进程间通信,可以结合`AIDL`(Android Interface Definition Language)和`Messenger`来实现,`Messenger`本质上也是基于`Handler`的跨进程消息传递机制。 总结,`Activity`中的多个`Handler`处理...
String result = doSomeBackgroundTask(); // 使用post()方法在UI线程更新UI handler.post(new Runnable() { @Override public void run() { // 更新UI,比如显示结果 textView.setText(result); } }); }...
在Android开发中,`Handler`是一个非常重要的组件,它用于处理UI线程之外的异步消息,实现线程间通信。`Handler`、`Looper`和`Message`三者共同构成了Android的消息处理机制,帮助开发者在不同的线程之间传递数据和...
在Android开发中,`Handler`、`Message`和线程是三个非常重要的概念,它们用于在应用程序的不同组件之间实现异步通信和数据传递。本文将深入讲解这些概念,并通过一个简单的实例来帮助初学者理解其工作原理。 首先...
Foxit PDF Preview Handler 是一款专为Outlook 2007设计的插件,它使得用户可以在不离开邮件客户端的情况下预览PDF附件。这款工具极大地提升了处理PDF文档的效率,尤其是在处理大量PDF邮件附件时,避免了频繁打开...
# Android Handler解析 在Android应用开发中,保持应用程序的响应性是至关重要的。为了实现这一目标,我们需要确保UI线程不会被阻塞。通常来说,将耗时的任务(如网络请求、复杂计算等)放到后台线程执行可以提高UI...
在Android应用开发中,Handler是一种重要的线程通信机制,它与Looper、Message紧密配合,用于在不同的线程间传递消息,解决多线程同步问题。本篇将详细讲解Handler的使用,包括如何创建Handler对象、如何发送和处理...
在Android应用开发中,Activity是用户界面的主要载体,而Handler机制则是实现异步通信和更新UI的关键工具。本文将深入探讨在一个Activity中如何管理和处理多个Handler以及它们的消息流程。 首先,Handler是Android...
【Android_Handler详解(一)】 在Android开发中,Handler是一个至关重要的组件,它与线程、消息队列和Looper紧密关联,用于实现不同线程间的通信。本篇将深入探讨Handler的基本概念、使用方法以及其在多线程环境中的...
在Android开发中,`Handler`、`AsyncTask`和`Looper`是三个非常重要的概念,它们主要用于处理应用程序的异步操作和线程通信。本文将深入探讨这些知识点,并通过实例来展示它们如何协同工作。 首先,`Handler`是...