Get part of text out of file ending with .setup.

I want to get a part of text out of a the file ending with .setup for a directory.

The files are forexample called:
test.setup
one.setup
three.setup

The folling text is in the files:

# NOTE: Required Fields -
# Name, Description, and Price must always be include
#

@link_fields = qw(ID Name Category Image Description Price Taxable);

# Define index value for product ID and Category ID
# This is the positiong in @link_fields array

The line: @link_fields = qw(ID Name Category Image Description Price Taxable); is for all not always the same.

for example:

test.setup contains the line:
@link_fields = qw(ID Name Category Image Description Price Taxable);

one.setup contains the line:
@link_fields = qw(ID Name Category Image Description Price Taxable test);

three.setup contains the line:
@link_fields = qw(ID Name Category Image Description Price Taxable Option);

I want to read all the original array values: ID Name Category Image Description Price Taxable (test Option)
from all the file to an array.
Double entries must be deleted from this array.

Can this be done?
weversbvAsked:
Who is Participating?
 
Kim RyanConnect With a Mentor IT ConsultantCommented:
This answer preserves the original order

$dirname = '.';
opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($file = readdir(DIR)))
{
    if ( $file =~ /\.setup$/ )
    {
        open(FH,"<$dirname/$file");
        @lines = <FH>;  # read file contents to array
        foreach $line ( @lines )
        {
            if ( $line =~ /^\@link_fields = qw\((.*)\);$/ )
            {
                push(@all_words,split(/ /,$1));
            }
        }
    }
}
closedir(DIR);

# Remove duplicate entries, while retaining order
@uniq_words = grep { ! $seen{$_} ++ } @all_words;
print @uniq_words;
0
 
Kim RyanIT ConsultantCommented:
I think this is what you need. Curious, to know why your setup files use perl syntax anyway. It may be easier to let perl load and process these files, depending on what else is in them.


$dirname = '.';
opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($file = readdir(DIR)))
{
    if ( $file =~ /\.setup$/ )
    {
        open(FH,"<$dirname/$file");
        @lines = <FH>;  # read file contents to array
        foreach $line ( @lines )
        {
            if ( $line =~ /^\@link_fields = qw\((.*)\);$/ )
            {
                @words = split(/ /,$1);
                foreach $word ( @words )
                {
                    # store in hash to remove duplicates
                    $uniq_words{$word} = 1;
                }
            }
        }
    }
}
closedir(DIR);

@unique_words = keys %uniq_words;
print(sort @unique_words);

0
 
weversbvAuthor Commented:
"Curious, to know why your setup files use perl syntax anyway. It may
be easier to let perl load and process these files, depending on what else is in them."

I use this method for the setup! and only therefor. Normaly I let perl load and process those file. (using require).

You solution is almost what I want. But the result must be sorted as found. The use of a hash in finding duplicates will sort the result in a strange way!

Can this be done an other way so the result will be as found?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.