Solved

how to read file and check condition in Perl

Posted on 2011-03-21
5
431 Views
Last Modified: 2012-08-14
Hi,

This is the sudo code for what I want to do:

my $gLogin = (grep { $_ ne 'myserve' } split /,/, $job->getNList)[0]; 
$gList =~ s/none\s*,?\s*//ig;               

 my @validClusters = read the config file for the list of clusters. 

Open in new window


The config file is a text file that has following in it:

config.txt
List of valid Cluster:
ABCD
KHSH
TGFHY
RTG
BNGH
A
%EOF
if ($cluster exist in @validClusters && $gLogin returns something from above && $gList returns something from above) and (option1 or option2 exists or both options exist)

then {do something}

else {don't do anything}

Open in new window



Thanks,
0
Comment
Question by:Tolgar
  • 2
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 500 total points
ID: 35183450
I'm not sure what option1 and option2 refer to, so I assume they are variable names.  I'm also assuming that "returns someting" and "exists" both mean that the value is defined.

my $gLogin = (grep { $_ ne 'myserve' } split /,/, $job->getNList)[0]; 
$gList =~ s/none\s*,?\s*//ig;               

open(my $CONFIG, "config.txt");
my @validClusters = map {chomp } <$CONFIG>;
close($CONFIG);

# Populate a hash to make the lookup easier.
my %validClusters = map { ($_ => 1) } @validClusters;

if ($validClusters{$cluster} && defined($gLogin) && defined($gList) && (defined($option1) || defined($option2)))
{
  #do something
} else {
  #don't do anything
}

Open in new window

0
 

Author Comment

by:Tolgar
ID: 35183534
I have a question:
Where is $cluster defined in this part of your code?

if ($validClusters{$cluster} ................

Thanks,
0
 

Author Comment

by:Tolgar
ID: 35189653
Also one more thing:

How is this part of the code going to neglect the fist line (List of valid Cluster:) in the text file which is a description only. If it is easier, I can put # in the beginning of comment lines. But then how will the code you sent me will distinguish it?

open(my $CONFIG, "config.txt");
my @validClusters = map {chomp } <$CONFIG>;
close($CONFIG);


Thanks,
0
 
LVL 8

Expert Comment

by:koppcha
ID: 35190004
use File::Slurp qw(slurp);
my @Clusters = slurp("config.txt");
delete $Clusters[0];

now @Clusters has all the clusters you are looking for



0
 
LVL 13

Assisted Solution

by:Carl Bohman
Carl Bohman earned 500 total points
ID: 35190839
I was using your pseudo-code as the basis for my code.  You never described what $cluster was supposed to be, so I was assuming that you were defining it somewhere.  If you are assuming that my code needs to define the variable, you are going to need to be describe what that variable is supposed to be/do.  The original question is too vague for me to be able to make a decent guess.

I did miss that first line.  If you want to explicitly skip the first line, you can do this:

open(my $CONFIG, "config.txt");
<$CONFIG>; # Read one line and discard it.
my @validClusters = map {chomp } <$CONFIG>;
close($CONFIG);

Open in new window


If you want to explicitly drop comments (marked with #), you can do this:

open(my $CONFIG, "config.txt");
my @validClusters = map {chomp } grep { ! /^\s*#/ } <$CONFIG>;
close($CONFIG);

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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