On Dec 3, 8:10 pm, ericmooshag...@gmail.com (Eric Mooshagian) wrote:

> I present some subroutines that  1) create an index for one or more  
> arrays and then 2) get summary statistics based on the index. I have 2  
> issues:
>
> 1. I would like to be able to create the index conditional on the  
> values of another array, e.g., conceptually something like:
>
> my @index = &defindex(\...@a,\...@b,\...@c)  for \...@dv >150 & dv < 800

I'm not sure since there are significant problems with the line
above.
Again I'm not sure but did you perhaps mean:

    my $size = @dv;
    my @index = definedx(  \...@a,\...@b,\...@c )
           if $size > 150  and  $size < 800;


Full strictures would expose some of the problems. Your code only
imports 'vars' and 'subs'. In other words, you used:

    use strict qw/vars subs/;   #  better alternative:  use strict;

With 'use strict;', you'd see warnings like those below::

     'Possible precedence problem on bitwise & operator at ...'

(You probably intended the lower precedence 'and' )

      'Bareword "dv" not allowed while "strict subs" in use  at ...'

(You probably intended @dv here but the condition itself
   is the more serious problem. )


Also, the prefix '&' is optional. See 'perldoc perlsub' for more
info.  Bottom line: it's a bad habit to include '&' automatically.

>
> However, I have no idea how to write the code to do this.   Ideally I  
> would be able to have several constraints (e.g., filter by reaction  
> time, accuracy, etc).  Any clues?  Is there a specific keyword or  
> topic I should search?
>
> 2. This following section is redundant across the subroutines, but I  
> cannot figure out how put it into another subroutine and return the  
> result in a way that I can use it. I think I'm just not understanding  
> how to correctly return/use the hash with multiple values per key. ( I  
> want to maintain separate subroutines for each measure)
>
>         my @ke...@{$_[0]};
>         my @valu...@{$_[1]};
>         my %combos = ();        
>
>         my $i=0;
>         foreach (@keys) {
>                 my $key=$keys[$i];
>                 my $value=$values[$i];
>
>                 push( @{$combos{$key}}, $value );
>                 $i++;
>         }

The above code can be replaced with:

my %combos;
@combos{ @{$_[0] }  } =  @{ $_[1] };  # a hash slice


Looks like there are other pitfalls too such as your use of
subroutine prototypes, etc.

I'd recommend reviewing some of the tutorials too such
as perlsub and perlref.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to