Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

23 Experts available now in Live!

Get 1:1 Help Now