in reply to Re^2: adding 1 to a char of 1
in thread adding 1 to a char of 1
For example:
my $index = 0; for (my $i = 0; $i < 100; $i++) { $index++; my $fullname = sprintf "base_%04d", $index; printf "fullname = '%s'\n", $fullname; }
will print 100 different names, starting with "name_0001" and ending with "name_0100". The %04d achieves this result; the "0" left-pads the integer value with zeroes, and the "4" gives you 4 decimal places.
Read up on sprintf to get more details: perldoc -f sprintf.
If you absolutely must get the last 4 digits of a string, you can use "substr" like this:
$last_4_chars = substr($my_string, -4);
This has the effect of taking the "rest" of the string, starting at the 4th character from the end (since the offset -4 is negative) rather than from the beginning.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: adding 1 to a char of 1
by Anonymous Monk on Jun 20, 2006 at 18:31 UTC |