Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to read file and check condition in Perl

Posted on 2011-03-21
5
Medium Priority
?
469 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
[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
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 2000 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

597 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