Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple question.
i have an array that has various kinds of information in it and (5) fields that i want to do something special with.

Now here is the question:
Currently my condition looks like this :
if (defined (@array[9..13]) and @array[9..13] ne "") { &my_sub (); }
The Problem with the above conditional is that it will only run &my_sub if all of the 5 array values have some value present in them.
This is what i want to fix. I want to run the sub even if (two or any number) of the five arrays are present.

I guess this will mean that i would have to look at each of these 5 first and do a loop of some kind on each one that is present. But when i tried to use a foreach my result screwed up.

Example

@array = ( name, address, age, date1, date2, date3, date4, date5); Take date1 .. date5; on each encountered date run spcial sub to process each date input(whi +ch you dont woory about).

Thanks Go-Juve

Replies are listed 'Best First'.
Re: simple array condition
by broquaint (Abbot) on Apr 03, 2002 at 18:26 UTC
    You could put grep() to good use in this situation e.g
    if(grep { defined and $_ ne "" } @array[9..13]) { &mysub(); ... } # or using your second example @array = qw(name address age date1 date2 date3 date4 date5); &your_function_here($_) for grep /date\d+/, @array;

    HTH

    broquaint

      Broquaint actually answered your questions as asked. Given the additional information that that wasn't really what you wanted, I am speculating you want to run your sub once for each of the elements in that range which is defined and non-null, for which an obvious implementation would be:
      &mysub() for (grep { defined and $_ ne "" } @array[9..13]);
Re: simple array condition
by particle (Vicar) on Apr 03, 2002 at 18:24 UTC
    i'm not exactly sure what you're looking for.
    will something like this work?

    if( defined @array[9..13] ) { for( @array[9..13] ) { my_sub( $_ ) if $_ ne ''; } }

    ~Particle ;Þ

      There's something very wrong happening or I am smoking crack.
      @array[9..13] is an array slice which evaluates to a list which in turn evaluates to its last component in scalar context!
      So defined @array[9..13] is just an equivalent of defined($array[13]), is it not???
        you are indeed correct. my code's been a little off lately. i'm a little out of practice, stuck in ksh all day. correct (but a little hard to read) would be:

        defined $_ && $_ ne '' && my_sub($_) for @array[9..13];

        ~Particle ;Þ

Re: simple array condition
by Silicon Cactus (Scribe) on Apr 03, 2002 at 20:25 UTC

    Based on your statement "on each encountered date run spcial sub to process each date input," it sounds to me like when you build your dataset, you should consider putting it into a hash. Have your keys be the actual data you want to juggle. You can then give the value for each key a "datatype" of DATE, NAME, AGE, etc.

    -pseudoexplanation of hash NOT PROPER CODE-
    my %Data; $Data{'Bob'} = "NAME"; $Data{'1234 Perl Way'} = "ADDRESS"; $Data{'5'} = "AGE"; $Data{'1/2/02'} = "DATE"; $Data{'3/4/02'} = "DATE";
    and so on

    I will admit, it is pretty crude, and counts on the fact that you know what data is going into the set, but hey, it would work for what you want, and it would be relative easy to iterate over.

    Oh, and it is likely to be viewed as a backwards way to use a hash. hee hee

Re: simple array condition
by Anonymous Monk on Apr 03, 2002 at 18:54 UTC
    Both of these are nut returning the correct data
      perhaps you could provide some data, or better explain your problem.

      ~Particle ;Þ