In a message dated 10/19/2005 9:13:01 P.M. Eastern Standard Time, [EMAIL PROTECTED] writes:
 
> [EMAIL PROTECTED] wrote:
> >
> > This is going to be a head-slapper, I know, but it's late and my brain
> > hurts... I've got a script that has a sub that accepts a hash of
> > arguments, one of which happens to be an array. If I pass the array as a
> > reference, everything works fine.
> >
> > Now for the head slapping part, how do I handle things INSIDE the sub so
> > I don't have to pass the array as a reference?  Code and desired output
> > are below.
> >
> > Thanks,
> >
> > Deane
> >
> > use strict;
> > use warnings;
> >
> > sub do_smthng{
> > my %args = ( -s1 => undef,
> >              -s2 => undef,
> >              -ar => undef,
> >              @_
> >            );
> >
> >    print "    s1: $args{-s1}\n";
> >    print "    s2: $args{-s2}\n";
> >    print "ary[0]: $args{-ar}->[0]\n";
> >    print "ary[1]: $args{-ar}->[1]\n";
> >    print "ary[2]: $args{-ar}->[2]\n";
> > }
> >
> >
> > my @arg_ary = ( 'alpha', 'bravo', 'charlie' );
> >
> > do_smthng( -s1 => 42,
> >            -s2 => 96,
> >            -ar => [EMAIL PROTECTED] ); # I wanna lose the "\" here
> >
> >
> > Desired output:
> >     s1: 42
> >     s2: 96
> > ary[0]: alpha
> > ary[1]: bravo
> > ary[2]: charlie
> >
> >
> One approach:
>
> use strict;
> use warnings;
>
> main();
>
> sub do_smthng
> {
> my $var = shift;
>
> print "s1 = $var->{s1}\n";
> print "s2 = $var->{s2}\n";
> for(my $c = 0; exists $var->{arg_ary}[$c];$c++)
>          {
>          print "arg_ary [$c] = $var->{arg_ary}[$c]\n";
>          }
> }
>
> sub main
> {
> my $x;
>
> @{$x->{arg_ary}} = ( 'alpha', 'bravo', 'charlie');
yikes!   in the next obfuscated perl contest, this is going to be a leading contender
in the ``best abuse of autovivification'' category.  
 
> $x->{s1} = 42;
> $x->{s2} = 96;
>
> do_smthng($x);
> }
>
> HTH - Lynn.
 
now my brane really hurts!  
 
bill walters  
 
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to