简体   繁体   中英

How do I splice a scalar variable?

$ab = "asljdhashdklasjkl ;dajs;ld";

I want the first 10 characters from $ab .

Can the splice function be used?

substr($ab, 0, 10)

在这里阅读有关substr函数的更多信息

Substr works great for your example. Splice operates on an array, so you would need to round trip it:

#!/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);
#! /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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM