Help with Perl If statement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with Perl If statement
# 1  
Old 04-26-2011
Bug Help with Perl If statement

Hi All,
I am writing a perl script where I take 2 variables from the user as STDIN to scan the lines of a file to produce as output.

How can I do an IF loop to test this for example in the mock file
Code:
12 10
35 20
37 5
45 12

if I take user input as 40 and 10, how can I get the output lines in which $F[0] has a value of 40 or more AND $F[1] has a value of 10 or more, to get output
Code:
45 12

I would also like to know how I can make it do the same for $F[0] >= 40 or more OR $F[1] >= 10 or more to get output:
Code:
12 10
35 20
45 12

Can someone enlighten on this ? I would appreciate Perl solutions as I am learning that.
Cheers and have a nice day
Smilie
# 2  
Old 04-26-2011
If schoolwork, there is a guideline to read and template required. How far did you get?
# 3  
Old 04-27-2011
@DGPickett:
This is not homework ... I have a personal interest in learning Perl as a skill to get some data analysis done in my routine work.
At the moment my code has two separate IF loops within the WHILE (<I>) as it reads the file, but I was wondering if there is a way to combine the two if statements.
I tried implementing
Code:
if ((condition_1) && (condition_2)){

and
Code:
if ((condition_1) || (condition_2)){

versions but didnt get the desired output.
Any other ideas how it can be done?
Cheers Smilie
# 4  
Old 04-27-2011
Well, You cannot do both tests at once, && is the logical and, but you did not show how you removed the digits from the line to test them. Print out the substrings as a debug to stderr. You may be testing a carriage return or the whole line or an empty string.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - what does this statement mean -Regex

push @MACARRAY, "$+{catalog} $+{machine}\n" if ($info =~ /(?<catalog>catalog).+?(?<machine>\*+)/ms); I am (still) trying to solve problem. Looking around on the server I found this piece of code. Specifically what does "$+{catalog} $+{machine}\n" do ? Thanks in advance (1 Reply)
Discussion started by: popeye
1 Replies

2. Shell Programming and Scripting

Understanding perl statement

can someone help me how to interpret this line? my ($class, $hashref) = @_; my $portfolio = {}; if ($hashref->{portfolio_id}) { ($portfolio) = GEmySQL->get ("select * from portfolio where portfolio.id=$hashref->{portfolio_id}"); } =============== Question: how do... (2 Replies)
Discussion started by: onlinelearner02
2 Replies

3. Shell Programming and Scripting

Perl nested if statement

I'm just having a bit of trouble running this code. It tells me that there's a syntax error on line 29. Any help appreciated. #!/usr/bin/perl # # Phone Book Application # %phonebook = ( "Wayne", '34687368', "Home", '378643287', "Work", '017374637', "School",... (2 Replies)
Discussion started by: cabaiste
2 Replies

4. Shell Programming and Scripting

Condition statement in perl

#!/usr/bin/perl $output1 = "/home/log.txt" $output2 = "/home/grep.txt" #Statement1 creates an output file called log.txt. #Statement2 greps a line from log.txt and store the result in grep.txt I want to create a condition where if the file grep.txt is empty repeat process. Thanks. (1 Reply)
Discussion started by: sureshcisco
1 Replies

5. Shell Programming and Scripting

Perl - automating if statement test

Hello all, I'm trying to automate an if statement in my Perl script. The script opens an input file for reading, checks each line in the file for a particular substring, and if it finds the substring, writes it to an output file. There are approximately 200 different input files. Each has... (3 Replies)
Discussion started by: Galt
3 Replies

6. Shell Programming and Scripting

help with if statement perl scripting

Sorry for the newbie question I need to create a perl script to check the modifed time of a file I already have that part created but need help in creating a if statement that basicly will check to see if the modifed time is at least 2hrs older then the current time if the stament is true to... (2 Replies)
Discussion started by: nettech207
2 Replies

7. Shell Programming and Scripting

Help with perl and if file statement

Hello and thank you in advance. I have a perl script that will basically copy files out of a directory , creat a new current date stamped dir and move them that current time stamped dir and append the file names with current time stamp. I need to add line(s) that this is only run if FileX... (1 Reply)
Discussion started by: Dadwith2boys
1 Replies

8. Shell Programming and Scripting

Diamond operator in Until Statement (perl)

Hello: I have the following perl script which is giving me trouble inside the second elsif statement. The purpose of the script is to go through a file and print out only those lines which contain pertinent information. The tricky part came when I realized that certain items actually spanned... (5 Replies)
Discussion started by: erichpowell
5 Replies

9. Shell Programming and Scripting

Problem with if statement in perl

if (($fields eq $hwp) && ($fields eq 'Y')) { $fields = "INTEGRAL"; } elsif ($fields eq $hwp) { $fields = "INTEGRAL"; } elsif ($fields ne $hwp) { $fields = "SEPARATE"; } print "$fields $fields $fields\n"; Output: The problem here is that the first... (2 Replies)
Discussion started by: kamitsin
2 Replies

10. Shell Programming and Scripting

perl if statement

Hello guys, I was wondering why my code doesn't read a variable when using if statement as follows: $userlist='users.txt'; $user='b999'; open (ACTULOG, ">>$userlist"); print ACTULOG "$user\n"; close (ACTULOG) this will work and prints... (3 Replies)
Discussion started by: Bashar
3 Replies
Login or Register to Ask a Question