Solved

how to read file and check condition in Perl

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

18 Experts available now in Live!

Get 1:1 Help Now