Solved

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

Posted on 2002-04-26
3
198 Views
Last Modified: 2010-03-05
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?
0
Comment
Question by:weversbv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 6972631
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
 

Author Comment

by:weversbv
ID: 6972829
"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
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 100 total points
ID: 6974328
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

617 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