in reply to Re^2: a referencable array slice (so assigning to it modifes the original)
in thread a referencable array slice (so assigning to it modifes the original)

Like I said, I can't test this, but I think I see an error in your code:

use strict; use warnings; use Data::Alias; use Data::Dumper; my @foo = qw( A T T G C T T G C T T C ); alias my @view = ( @foo[1,2], @foo[5,6] ); alias push @view, @foo[7,8]; # Added alias here. map {$_='$'} @view; print Dumper \@foo; print "nok" if $foo[7] ne '$';

You added copies of $foo[7] and $foo[8] to @view. So you wound up with something like:

@view |- 0: alias $foo[1] |- 1: alias $foo[2] |- 2: alias $foo[5] |- 3: alias $foo[6] |- 4: 'G' \- 5: 'C'
When you wanted:
@view |- 0: alias $foo[1] |- 1: alias $foo[2] |- 2: alias $foo[5] |- 3: alias $foo[6] |- 4: alias $foo[7] \- 5: alias $foo[8]

I haven't been able to play with this, but it feels like you need to think about each scalar as either a normal value or an alias--unless you are working with whole aggregate aliases, in which case the aggregate is aliased. If my understanding is correct, in the example above we are not using whole aggregate aliases (just a slice), so individual aliases are assigned to @view.


TGI says moo

Replies are listed 'Best First'.
Re^4: a referencable array slice (so assigning to it modifes the original)
by muenalan@cpan.org (Initiate) on Jun 18, 2008 at 18:47 UTC
    Your right. Corrected it, but still same problem.
    use strict; use warnings; use Data::Alias; use Data::Dumper; my @foo = qw( A T T G C T T G C T T C ); alias my @view = ( @foo[1,2], @foo[5,6] ); map {$_='x'} @view; print Dumper \@foo; alias my @subview = ( @foo[7,8] ); push @view, @subview; map {$_='$'} @view; print Dumper \@foo; print "nok" if $foo[7] ne '$';
    I am not sure if an alias is something you can push and pop on thing ? So how to modify a @view once its there ?

      What exactly are you trying to achieve? Do you want to be able to take an alias to a contiguous slice of one array and then be able to expand the aliased view, and have the original expand as well?

      original: (1, 2, 3, 4, 5) | | alias: (2, 3, 4) push @alias, (a,b,c); alias: (2, 3, 4, a, b, c) | | original: (1, 2, 3, 4, a, b, c, 5)

      If that's the behavior you are looking for Data::Alias won't do it. An array slice can refer to any set of indexes in any order from the original. So it is impossible to define where the start and end of the array are within the orignal array. @foo = @bar[5,1,1,5] is perfectly legal. Where do shift and push operate on @foo if it is aliased?

      You could probably make something like this work using a tie or something. You'd need to be careful about keeping the idea of start and end of array very clear. I doubt you will find something already written to handle this case.


      TGI says moo