Solved

how to read file and check condition in Perl

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now