2 Replies - 857 Views - Last Post: 15 December 2008 - 07:43 AM

#1 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Perl string comparsion problem

Posted 14 December 2008 - 12:00 PM

Hi! I have to write a little code that calculate amount of alcohol drinked. Script should ask for amount of alcohol and type of beverage, and calculate amount of alcohol. Here's my code:

#!/usr/bin/perl
do
{
	print "Podaj ilosc alkoholu: ";
	$ilosc = <>;
	print "Podaj typ alkoholu: ";
	$typ = <>;
	if($typ eq "piwo")
	{
		print "Ilosc spozytego alkoholu: " . $ilosc*(5.5/100) . " gramow";
	}
	elsif($typ eq "wodka")
	{
		print "Ilosc spozytego alkoholu " . $ilosc*(40/100) . " gramow";
	}
	elsif($typ eq "wino")
	{
		print "Ilosc spozytego alkoholu ". $ilosc*(12/100) . "gramow";
	}
	else
	{
		print "Nie wiem co to za alkohol";
	}
	print "\nZakonczyc? Wpisz `koniec` aby zakonczyc: ";
	$koniec = <>
} while ($koniec eq "koniec")



The problem is, string comparsion never passes! I always get answer from `else` statement :/ I really don't know why ... Thanks for answers.

EDIT:

My friend solved the problem. I have to compare strings with "\n" at the end ;) For example if($typ eq "piwo\n") ;)

This post has been edited by k0b13r: 14 December 2008 - 12:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Perl string comparsion problem

#2 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Perl string comparsion problem

Posted 14 December 2008 - 11:41 PM

The standard way to do what you are doing is to use chomp() to remove the newline instead of putting the newline into the comparison:

chomp($typ = <>);

then do your comparison
Was This Post Helpful? 1
  • +
  • -

#3 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Perl string comparsion problem

Posted 15 December 2008 - 07:43 AM

Oh, good to know. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1