Solved

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

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

739 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