Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture first 8 characters Perl

Tags:

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?

like image 623
Aleksandra Sretenovic Avatar asked Aug 01 '13 16:08

Aleksandra Sretenovic


People also ask

How do I get the first letter of a string in Perl?

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.

How do I extract a character from a string in Perl?

substr() in Perl returns a substring out of the string passed to the function starting from a given index up to the length specified. This function by default returns the remaining part of the string starting from the given index if the length is not specified.

What is $@ Perl?

$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.

What does \b do in Perl?

\b is the backspace character only inside a character class. Outside a character class, \b alone is a word-character/non-word-character boundary.


1 Answers

Use substr.

print substr($word, 0, 8);
like image 88
ikegami Avatar answered Oct 27 '22 18:10

ikegami