Solved

how to read file and check condition in Perl

Posted on 2011-03-21
5
460 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

707 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