tags:

views:

78

answers:

3
$ab = "asljdhashdklasjkl ;dajs;ld";

I want the first 10 characters from $ab.

Can the splice function be used?

+6  A: 
substr($ab, 0, 10)

Read more about the substr function here

Amadan
use 4-arg substr instead of lvalue substr
Nikhil Jain
This was intended as a rvalue. He wants to get the first 10 chars, not replace them.
Amadan
A: 
#! /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>
Vijay Sarathi
The `print` statement won't work unless you have the array slice in double quotes. Even then, it'll print the characters space-separated by default.
Zaid
@Zaid..i have tested this and its working.Please check whether you are correct in your statement!!!
Vijay Sarathi
A: 

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);
10rd_n3r0