如何拼接标量变量?

时间:2010-07-30 08:51:57

标签: perl

$ab = "asljdhashdklasjkl ;dajs;ld";

我想要来自$ab的前10个字符。

可以使用splice功能吗?

3 个答案:

答案 0 :(得分:6)

substr($ab, 0, 10)

详细了解substr函数here

答案 1 :(得分:0)

Substr非常适合您的示例。 Splice在阵列上运行,所以你需要往返它:

#!/usr/bin/perl
my ($ab, @ab, @first_ten, $first_ten);

$ab = "asljdhashdklasjkl ;dajs;ld";

@ab = split(//, $ab);
@first_ten = splice(@ab, 0, 10);

$first_ten = join('', @first_ten);

答案 2 :(得分:-2)

#! /usr/bin/perl 
use strict; 
my $info = "asljdhashdklasjkl ;dajs;ld";
my @personal = split(//, $info);
print @personal[0..9];


C:\Documents and Settings\Administrator>perl perltest.pl
asljdhashd
C:\Documents and Settings\Administrator>
C:\Documents and Settings\Administrator>type perltest.pl
#! /usr/bin/perl
use strict;
my $info = "asljdhashdklasjkl ;dajs;ld";
my @personal = split(//, $info);
print @personal[0..9];
C:\Documents and Settings\Administrator>