Using foreach and multiple arrays

Posted on 2006-03-31
Last Modified: 2012-05-05
I wrote a function in perl where it reads in each file in a directory and checks for a tag:

Each file should have one of the sequences of tes tags as defined in these arrays.
I am trying to do the following - if the tag sequence in the match does not follow any one of the arrays, then continue onto the next array to see if there is a match there.

I could possibly store the tag values from the file into an array then compare.  But the problem is that I only need to match the first 4 or 5  tags.  Each file consists of 10 - 15 tags.

What would be the best way to do this?

@testarray1 = qw (QW1, LS3, DE3, TER);
@testarray2 = qw (GF3, LS3, RTL, ST);
@testarray3 = qw (LS5, R3E, DE3, EEW);
@testarray4 = qw (FFL, DRE, ASD, FGE, GFH, SDF);

open(FILE, "$filename")
#read all lines
  foreach $entry (@testarray1)
    if (/\<tes/ && /type=\"$entry\"/)
          //matches!  check the next entry
         //stop checking this array, and check the next array
Question by:jewee
    LVL 84

    Accepted Solution

    my $n=0;
    for( \@testarray1, \@testarray2, \@testarray3, \@testarray4 ){
       push @test,{map{$_,$n}@$_}
    {local @ARGV=grep -f,<directory/*>;
     while( <> ){
        if( /<tes/ and my($entry)=/type="([^"]+)"/ ){
           for( @test ){ if( my $n=$_->{$entry} ){ print "$ARGV matches array $n\n" }
    LVL 84

    Expert Comment

    perl -Mdiagnostics -w -e "@testarray1 = qw (QW1, LS3, DE3, TER);"
    Possible attempt to separate words with commas at -e line 1 (#1)
        (W qw) qw() lists contain items separated by whitespace; therefore
        commas aren't needed to separate the items.  (You may have used
        different delimiters than the parentheses shown here; braces are also
        frequently used.)
        You probably wrote something like this:
            qw! a, b, c !;
        which puts literal commas into some of the list items.  Write it without
        commas if you don't want them to appear in your data:
            qw! a b c !;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video discusses moving either the default database or any database to a new volume.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now