Perl nested if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl nested if statement
# 1  
Old 04-11-2011
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.

Code:
#!/usr/bin/perl
#
#    Phone Book Application
#

%phonebook = (
    "Wayne", '34687368',
    "Home", '378643287',
    "Work", '017374637',
    "School", '36328323',
    "Gym", '37623938'
);

print "Phonebook Menu.\n";
print "Option 1: Display a phone number.\n";
print "Please select an option [1-1]:";
$option = <STDIN>;
chop $option;
if(option eq "1"){
    print "Please enter a name.\n";
    $usr = <STDIN>;
    chop $usr;
    if($usr =~ /Wayne|Home|Work|School|Gym/){
        print "The phone number for $usr is $phonebook{$usr}.\n";
    }
    else{
        print "No such entry.\n";
    }
else{
    print "Invalid option.\n";
}

# 2  
Old 04-11-2011
Code:
#!/usr/bin/perl
#
#    Phone Book Application
#

%phonebook = (
    "Wayne", '34687368',
    "Home", '378643287',
    "Work", '017374637',
    "School", '36328323',
    "Gym", '37623938'
);

print "Phonebook Menu.\n";
print "Option 1: Display a phone number.\n";
print "Please select an option [1-1]:";
$option = <STDIN>;
chop $option;
if(option eq "1"){
    print "Please enter a name.\n";
    $usr = <STDIN>;
    chop $usr;
    if($usr =~ /Wayne|Home|Work|School|Gym/){
        print "The phone number for $usr is $phonebook{$usr}.\n";
    }
    else{
        print "No such entry.\n";
    }
}else{
    print "Invalid option.\n";
}

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-11-2011
Thanks, dunno how I didn't notice that! I was also missing a $ on the initial if statement Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert CSV file to nested XML file using UNIX/PERL?

we have a CSV which i need to convert to XML using Perl or Unix shell scripting. I was able to build this XML in oracle database. However, SQL/XML query is running for long time. Hence, I'm considering to write a Perl or shell script to generate this XML file. Basically need to build this XML... (3 Replies)
Discussion started by: laknar
3 Replies

2. Shell Programming and Scripting

Perl- Nested 'for' order change

Hello, I'm quite new to perl so my question is rather basic and I know there is probably a simple way around it but I can't seem to find it. I have a medium-length code and there is a part that works with a nested for loop: foreach my $j(@primpiddelta){ for (my $k=1;... (0 Replies)
Discussion started by: acsg
0 Replies

3. UNIX for Dummies Questions & Answers

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 12 10 35 20 37 5 45 12if I take user input as 40 and 10, how can I get the output lines in... (3 Replies)
Discussion started by: pawannoel
3 Replies

4. Shell Programming and Scripting

If-statement nested in case

I'm trying to write case statements with 'if statements' embedded inside of them. I'm using the korn shell but it's not functioning. If I want to see if a string exists in a file and then perform an action, what would be the best way to do this? For file "asg51fin" to delete a line if a... (1 Reply)
Discussion started by: dazeman27
1 Replies

5. Shell Programming and Scripting

Perl nested array problem

I have a array reference which has some number of array references inside it.The nested array references also contains the array references. my $Filename = "sample.xml"; my $Parser = new XML::Parser( Style => 'tree' ); my $Tree = $Parser->parsefile( $Filename ); Here the $Tree is the... (6 Replies)
Discussion started by: karthigayan
6 Replies

6. Shell Programming and Scripting

Perl - nested substitutions

How can I nest substitutions ? My solution just seems cheap ... sample data Cisco Catalyst Operating System Software, Version 235.5(18) Cisco Catalyst Operating System Software, Version 17.6(7) Cisco Catalyst Operating System Software, Version 19.6(7) Cisco Catalyst Operating System... (1 Reply)
Discussion started by: popeye
1 Replies

7. UNIX for Dummies Questions & Answers

Nested If statement within Do / Done

Hi all! I'm really hoping you can help me out here; now i have searched and searched and have at least worked out that you can't have a nested if statement with a 'done' in it (as i have) as you're killing the parent before the child. So here's what i have, and here's hoping someone can help... (2 Replies)
Discussion started by: dalgibbard
2 Replies

8. Shell Programming and Scripting

Nested foreach in perl

I have two arrays @nextArray contains some files like \main\1\Xul.xml@@\main\galileo_integration_sjc\0 \main\1\PortToStorageDialog.xml@@\main\galileo_integration_sjc\0 . . . \main\1\PreferencesDialog.xml@@\main\galileo_integration_sjc\0 @otherArray contains some files like ... (2 Replies)
Discussion started by: nmattam
2 Replies

9. Shell Programming and Scripting

while read loop w/ a nested if statement - doesn't treat each entry individually

Hi - Trying to take a list of ldap suffixes in a file, run an ldapsearch command on them, then run a grep command to see if it's a match, if not, then flag that and send an email alert. The list file (ldaplist) would look like - *********** o=company a o=company b *********** **... (7 Replies)
Discussion started by: littlefrog
7 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