In C, your windows might be implemented using pointer arithmetic.
const char* s = str+1;
const char* e = str+len;
for (const char* p=s; p!=e; ++p) putc(*p);
Except pointer arithmetic wouldn't allow you to resize the buffer (push @char_array, 'x';
). Even in C, you'd have to use offsets.
size_t si = 1;
size_t ei = len;
for (size_t i=si; i!=e1; ++i) putc(str[i]);
This is fortunate, because Perl doesn't have pointers, much less pointer arithmetic. But offsets? No problem!
my @char_array = split //, 'helloworld';
my ($s, $e) = (1, $#char_array);
say @char_array[$s..$e]; # elloworld
shift @char_array;
push @char_array, 'x';
say @char_array[$s..$e]; # lloworldx
If we're actually talking about chars, a string would be more efficient.
my $char_array = 'helloworld';
my ($s, $e) = (1, length($char_array));
say substr($char_array, $s, $e-$s+1); # elloworld
$char_array =~ s/^.//s;
$char_array .= 'x';
say substr($char_array, $s, $e-$s+1); # lloworldx
In fact, if we're actually talking about chars, we're quite lucky since we can use an lvalue substr and let Perl handle the offsets for us!
my $char_array = 'helloworld';
my $substr_ref = \substr($char_array, 1, length($char_array)-1);
say $$substr_ref; # elloworld
$char_array =~ s/^.//s;
$char_array .= 'x';
say $$substr_ref; # lloworldx
Way easier than C with more or less all the same benefits!