Solved

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

Posted on 2002-04-26
3
158 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

12 Experts available now in Live!

Get 1:1 Help Now