Capture first 8 characters Perl

How could I take 8 characters of a string, for example:

If I has like this:

my $word ="take first 8 characters";

How could I print this: take fir?

Answers


Use substr.

print substr($word, 0, 8);

Use the built in substr function. substr($string, $start, $length)


From perlfaq4: How can I access or change N characters of a string?:

You can access the first characters of a string with substr(). To get the first character, for example, start at position 0 and grab the string of length 1.

my $string = "Just another Perl Hacker";
my $first_char = substr( $string, 0, 1 ); # 'J'

(See the rest of the article for guidance on changing part of a string.)


Need Your Help

How to kill all processes with a given partial name?

linux bash posix

I want to kill all processes that I get by:

Why is is_lock_free a member function?

c++ c++11 std atomic

What is the reason for why is_lock_free requires an instance (it's a member function)? Why not a metafunction of the type, or a static constexpr member function?