Perl : not getting the exact output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : not getting the exact output
# 1  
Old 02-27-2013
Perl : not getting the exact output

I have written a perl code to read alphabet from console and prints the status.
But every time it is printing "Entered is vowel".

Code:
print "Enter any alphabet: ";
$a = <STDIN>;

if ( $a == "a" || $a == "e" || $a == "i" || $a == "o" || $a == "u" )
{
    print "entered is vowel";
}    
else
{
    print "not a vowel\n";
}

# 2  
Old 02-27-2013
Code:
print "Enter any alphabet: ";
$a = <STDIN>;
chomp ($a);
if ( $a eq "a" || $a eq "e" || $a eq "i" || $a eq "o" || $a eq "u" )
{
    print "entered is vowel\n";
}
else
{
    print "not a vowel\n";
}

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 02-27-2013
Thanks balajesuri...

But why are we using chomp here...

Regards,
J
# 4  
Old 02-27-2013
to "chomp" the new line character (which is the last character appended to the user input ).
# 5  
Old 02-27-2013
Quote:
Originally Posted by scriptscript
But why are we using chomp here...
When you enter an alphabet (say x) on command line and hit the enter key, $a actually is "x\n" (on *nix like systems). To chomp off the new-line character at the end of any string, you use the routine "chomp".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace exact word by perl

my inputfile: This is a test and only a test for production - prod. echo "This is a test and only a test for production - prod" | perl -pi -e 's/prod/test/g' outputfile: This is a test and only a test for testuction - test I only want to replace prod, not production. I also... (3 Replies)
Discussion started by: loktamann
3 Replies

2. Shell Programming and Scripting

Grep the exact process in perl script

I am working on one script where I need to grep the exact process in perl script. for e.g. when I run simple grep command on the linux machine it gives me two process mGateway_mvc_q01 and mGateway_mvc_q01_v7 which is not the correct result.I tried to use ( ps -eAf | grep ^mGateway_mvc_q01$) but... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

3. Shell Programming and Scripting

How to take exact word from output.?

Hi i am writing and i want to take very first word "A924A5FC"from the below o/p A924A5FC 0910055313 P S SYSPROC SOFTWARE PROGRAM ABNORMALLY TERMINATED A924A5FC 0908091913 P S SYSPROC SOFTWARE PROGRAM ABNORMALLY TERMINATED A924A5FC 0906090313 P S SYSPROC SOFTWARE... (4 Replies)
Discussion started by: scriptor
4 Replies

4. Shell Programming and Scripting

Perl : printing the files in the exact order as it is..

Hi folks, I am trying to open directory and then print the files in that directory in PERL. I have used the below code. opendir(DIR,$destination) or die ("Cannot open directory $destination"); my @bwfiles = readdir(DIR); print @bwfiles; I am able to retrieve all the files the files... (2 Replies)
Discussion started by: scriptscript
2 Replies

5. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

6. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

7. Shell Programming and Scripting

PERL: Help required exact match

Hello, My requirement is to iterate over all the lines of a file and compare them with a word and perform some operations if exact match is found. For the snippet below, it works even if contents of line include "diff" and "diff:". I want it to work only if it is exactly "diff" and is not... (2 Replies)
Discussion started by: sarbjit
2 Replies

8. Shell Programming and Scripting

Issues with an exact match using regex in perl!

Hello Guys, I am trying to make an exact match for an email address entered as an argument, using perl, however, it's not working if I put a "$" in the email address. See the below outputs, Correct Match : bash-2.03$ echo sandy@test.com | perl -wln -e 'print if /(^*\@test.com$)/i'... (6 Replies)
Discussion started by: suffisandy
6 Replies

9. Shell Programming and Scripting

exact match in Perl

Hi By using select clause I'm trying to pull out the rows to a variable. If the variable has 0 row(s) selected then i'm printing some text message else printing some other text message if($xyz =~ m/0 row/) { print "0 rows "; } else { print " There are rows"; } By my problem... (4 Replies)
Discussion started by: pdreddy34
4 Replies

10. Shell Programming and Scripting

perl exact match

How to emulate grep -o option in perl. I mean to print not all line, only the exact match. echo "2A2 BB" | perl -ne 'print if /2A2/' 2A2 BB I want to print only 2A2. (2 Replies)
Discussion started by: mirusnet
2 Replies
Login or Register to Ask a Question