in reply to Re: Are strings lists of characters?
in thread Are strings lists of characters?

package StringArray; require Tie::Array; use base 'Tie::Array'; sub TIEARRAY { bless $_[1], $_[0] } sub FETCH { substr(${$_[0]}, $_[1], 1) } sub STORE { substr(${$_[0]}, $_[1], 1) = $_[2] } sub FETCHSIZE { length(${$_[0]}) } sub STORESIZE { $$self = substr(${$_[0]}, 0, $_[1]) } sub DELETE { substr(${$_[0]}, $_[1], 1) = '' } 1;
Example:
#!perl -w use StringArray; use strict; my $test = "Hello dolly"; my @testa; tie @testa, 'StringArray', \$test; print "\$#testa = $#testa\n"; print "testa[1] = $testa[1]\n"; $testa[1] = 'b'; print "testa[1] = $testa[1]\n"; map {$_ = uc $_} @testa; print "after map: test = $test\n"; delete $testa[1]; print "after delete: test = $test\n"; push @testa, "C"; print "after push: test = $test\n";

Replies are listed 'Best First'.
Re^3: Are strings lists of characters?
by Aristotle (Chancellor) on Oct 29, 2002 at 22:52 UTC
    Nope - still the same problem.
    #!/usr/bin/perl -w use strict; $_ = "converted"; tie my @test, 'StringArray', $_; print map "$_\n", map { delete @$_[2,5]; join '', grep defined, @$_; } \@test, [ /(.)/sg ]; package StringArray; require Tie::Array; use base 'Tie::Array'; sub TIEARRAY { my $str = pop; bless \$str, shift } sub FETCH { substr(${$_[0]}, $_[1], 1) } sub STORE { substr(${$_[0]}, $_[1], 1) = $_[2] } sub FETCHSIZE { length(${$_[0]}) } sub STORESIZE { $$_[0] = substr(${$_[0]}, 0, $_[1]) } sub DELETE { substr(${$_[0]}, $_[1], 1) = '' } 1; __END__ covered coveted

    Makeshifts last the longest.