in reply to Getting a list of aliases to selected items from a list of array references

I think I've found what I would consider an "elegant" solution to this.

Update: Apologies - my test was flawed - I copied & pasted the lines without intended modifications.

The "map_twice" code does not work as intended- it does not provide the simple, reference-free interface I'm looking for (Second map is unnecessary, and this case degerates to the same as the "ref" case). That code was not being tested.

The code below incorporates previous code on this topic, including tinita's suggestion (replacing 'map' with 'for', since the returned array is not used.)

I'm calling the new method "map_twice", which will now maintain aliases, and pass them into the subrouting parameter. No complications with closures and lvalu subs.

sub map_With_Ref { # kvale ... my( $sub )= shift( @_ ); &$sub( map { \$_->[0] } @_ ); # ^^^^^^^^^^^^^^^^^^ Here is the selection code } sub map_With_Closure { # NetWallah my( $sub )= shift( @_ ); my @closures; foreach my $aref(@_){ push @closures, sub :lvalue{ $aref->[0] }; } &$sub( @closures ); # I had trouble attempting to dynamically build the equivalent of +the # @closures array using the "map {BLOCK}" syntax - apparently that # conflicts with lvalue subs. } sub map_pass_as_Param{ #tinita's code... my( $sub )= shift( @_ ); &$sub( $_->[0]) for @_; # Call once for each param passed } sub map_twice{ # NetWallah my( $sub )= shift( @_ ); &$sub( map {$_} map { \$_->[0] } @_ ); } my @a= ( 1..3 ); my @b= ( 4..6 ); my @c= ( 7..9 ); sub addten_Ref { $$_ += 10 for @_ } sub addten_With_Closure { &$_() += 10 for @_ } sub addten_call_Sub { $_[0]+=10} # Only ONE param passed, multiple cal +ls sub addten_array { $_ += 10 for @_ } print "--- REF test --\n"; map_With_Ref( \&addten_Ref, \@a, \@b, \@c ); map_With_Ref( sub { print "$$_\n" for @_ }, \@a, \@b, \@c ); print "--- Closure test --\n" ; map_With_Closure( \&addten_With_Closure, \@a, \@b, \@c ); map_With_Closure( sub { print &$_() . "\n" for @_ }, \@a, \@b, \@c ); print "--- Calling Passed Sub test --\n"; map_pass_as_Param( \&addten_call_Sub, \@a, \@b, \@c ); map_pass_as_Param( sub { print shift . "\n" }, \@a, \@b, \@c ); print "--- Double-mapping test --\n"; # "Map-twice" should have been called here **** map_pass_as_Param( \&addten_array, \@a, \@b, \@c ); map_pass_as_Param( sub { print "@_\n" }, \@a, \@b, \@c );
--- OUTPUT---- --- REF test -- 11 14 17 --- Closure test -- 21 24 27 --- Calling Passed Sub test 31 34 37 --- Double-mapping test -- 41 44 47

Offense, like beauty, is in the eye of the beholder, and a fantasy.
By guaranteeing freedom of expression, the First Amendment also guarntees offense.

Replies are listed 'Best First'.
Re: Re: Getting a list of aliases to selected items from a list of array references
by ysth (Canon) on May 16, 2004 at 06:14 UTC
    I had trouble attempting to dynamically build the equivalent of the @closures array using the "map {BLOCK}" syntax - apparently that conflicts with lvalue subs. }
    sub : attribtute ... will be parsed at the beginning of a statement as "sub" being a label, and attribute... the code to run. Put a + in front of sub to disambiguate. Witness:
    $ perl5.8.4 -we'sub xyzzy { print "huh?" } sub : xyzzy; shift() and go +to "sub"' 1 2 huh?huh?huh?
    The makings of a fine japh here...
      Excellent! - Many thanks, ysth - that worked great!
      # Replace @closures usage by this line in "sub map_With_Closure" &$sub( map {my $x=$_; +sub:lvalue{$x->[0]} }@_ ); # "+" required in front of sub, to avoid perl's mininterpration # of "sub" being a label, and attribute-- thanks ysth!
      The only minor gripe with this code is that it requires the use of the temporary variable $x , since $_ is local to the lvalue sub, and does not inherit from the "map".

      Offense, like beauty, is in the eye of the beholder, and a fantasy.
      By guaranteeing freedom of expression, the First Amendment also guarntees offense.