Stephan Hochhaus wrote:

A question I assume can be answered using regexp, unfortunately I am just starting my way into it. I have a bunch of words that I want to split, so that the first letters (minus n) and the last n-letters are seperated.
n is user defined and therefore not static.


How can I have Perl split at the last n-letters from the end of a word?

You don't need to use regular expressions to accomplish this:

my $n = 3;
my $w = 'abcdefg';
print substr($w,-$n),"\n"; # prints: efg

You can do it with regular expressions, but I think is slower than taking the substring. For example:

my $n = 3;
my $w = 'abcdefg';
$w =~ /([:alpha:]*)([:alpha:]{$n})$/;
print "($1) ($2)\n"; # prints: (abcd) (efg)

Any help is appreciated (hints as where to find the answer myself without buying a book are also very welcome!)

'perldoc -f substr' for substr().
'perldoc perlre' for regular expressions.

Stephan




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to