perl: string comparison as numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users perl: string comparison as numbers
# 1  
Old 05-24-2007
perl: string comparison as numbers

$name = 'Mark';

$goodguy = 'Tody';

if ($name == $goodguy) {
print "Hello, Sir.\n";
} else {
print "Begone, evil peon!\n";
}

The output is Hello, Sir......

Even if the Mark is made to MarkSpencer.....then also it shows as Hello, Sir....

Can any one explain the ambiguity
# 2  
Old 05-24-2007
== is for numerical comparison. eq is for string comparison
Code:
if ($name eq $goodguy) {

# 3  
Old 05-24-2007
Some extra information: using == in this context actually makes Perl try to compare 0 == 0, which must be true. Why 0? Because strings like 'Mark' cannot be converted to number, which is regarded as 0. That explains why basically the == tends to report true for testing entirely different things.
# 4  
Old 05-24-2007
Hi.

Modifying your code slightly:
Code:
#!/usr/bin/perl

# @(#) p1       Demonstrate value of running with warnings on.

use warnings;

$name = 'Mark';

$goodguy = 'Tody';

if ( $name == $goodguy ) {
    print "Hello, Sir.\n";
}
else {
    print "Begone, evil peon!\n";
}

exit(0);

Produces:
Code:
% ./p1
Argument "Tody" isn't numeric in numeric eq (==) at ./p1 line 11.
Argument "Mark" isn't numeric in numeric eq (==) at ./p1 line 11.
Hello, Sir.

Another good module is strict, q.v. ... cheers, drl
# 5  
Old 05-25-2007
You've all pointed out why it doesn't work but not answered the implied question:

How to create the ascii numeric value for characters? Or more likely an array of numbers base 10 from a string. I don't know how to do this, and I don't have time to try to find out.

In C, this is valid and makes sense:
Code:
if ( 'c' >  13 ) 
{
    /* do stuff */
}

He is asking an analagous question.

Can some help? I'd like to know, too.
# 6  
Old 05-26-2007
Am not sure whether am answering your question or not !

I believe you would be knowing the reply I post.

Or, it should be that I misunderstood your question.

But anyways my try is here,

Code:
my $a = "fine";

print " $a : ord : " . ord($a) . "\n";

my $length = length($a);
for ( my $i = 0; $i <= $length; $i++ ) {
  my $c = substr($a, $i, 1);
  print " char: $c ord: " . ord($c) . "\n";

}

# 7  
Old 05-26-2007
Hi.

My take on this is that, as with many things in perl, there is more than one way of doing it. So, you can certainly use function ord as matrixmadhan has shown.

However, like the (Bourne family) shell , there is also an alternate set of relational operators for comparing strings. Here is a (longish) example:
Code:
#!/usr/bin/perl

# @(#) p3       Demonstrate comparison operators.

use warnings;
use strict;

my($first) = "b";
my($last) = "a";

if ( $first == $last ) {
        print " Value $first seems equal to $last with \"==\"\n\n";
}

if ( ord($first) > ord($last) ) {
        print " ords of value $first seems more than $last with \">\"\n\n";
}

if ( $first gt $last ) {
        print " Value $first seems more than $last with \"gt\"\n\n";
}

# Do a few comparisons.

comparison( "aa", "ab");
comparison( "ab", "able");
comparison( "ac", "able");
comparison( "1", "2");
comparison( "11", "2");
comparison( "1 begins this string", "2");

sub comparison {
        my($a,$b) = @_;

        if ( $a le $b ) {
                print " string :$a: is less than string :$b:\n\n";
        } else {
                print " string :$a: is greater than string :$b:\n\n";
        }
}

exit(0);

Which produces:
Code:
% ./p3
Argument "a" isn't numeric in numeric eq (==) at ./p3 line 11.
Argument "b" isn't numeric in numeric eq (==) at ./p3 line 11.
 Value b seems equal to a with "=="

 ords of value b seems more than a with ">"

 Value b seems more than a with "gt"

 string :aa: is less than string :ab:

 string :ab: is less than string :able:

 string :ac: is greater than string :able:

 string :1: is less than string :2:

 string :11: is less than string :2:

 string :1 begins this string: is less than string :2:

For me, this follows the principle of least surprise. See man perlop for details on the operators ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a matched pattern and perform comparison on numbers next to it

Hi, I have been trying to extract rows that match pattern "cov" with the value next to it to be > 3. The 'cov' pattern may appear either in $3 or $4 (if using ";" as field separator). Below is the example:- input file ... (7 Replies)
Discussion started by: bunny_merah19
7 Replies

2. Shell Programming and Scripting

Comparison of floating point numbers in bash

I have the following code snippet in bash if ]; then minm=`echo "$diff" | bc` fi It works well for most of the cases. However lets say diff is -0.17 and minm is -0.0017. In such a case the comparison seems to fail. Is the correct way to compare a mixture of positive and... (12 Replies)
Discussion started by: ngabrani
12 Replies

3. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

4. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

5. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Truncate all characters and numbers in string - using perl

hi, I have an data from file where it has 20110904 234516 <<hdd-10#console|0c.57,passed,5,28,READ,0,20822392,8,5,4,0,40,0,-1,0,29909,25000,835,3.3,0,0,0,0,implied,0,0,2011/9/5-2:3:17,2011/9/5-2:3:47,X292_0F15,TAP ,NQ09,J40LTG\r\r\n I want to remove characters till #console| i.e want... (1 Reply)
Discussion started by: asak
1 Replies

7. Shell Programming and Scripting

numbers comparison in fields of a file and print least value of them

Hi , I'm trying to compare fields in the file, I want compare the numbers in each column and get the least value of it. > cat input_file 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -0.2050 -0.6629 -0.6407 -0.6599 -0.4085 -0.3959 -0.2526 -0.3597 0.3439 0.2275 0.2780 ... (5 Replies)
Discussion started by: novice_man
5 Replies

8. Shell Programming and Scripting

String comparison

Hi, I have the below logic. Here 'X' is a variable having some string. if then echo "i dont need to go to ofc" else echo "i need to go to ofc" Please help me to write it in unix. Thanks. (2 Replies)
Discussion started by: 46019
2 Replies

9. Programming

comparison between float numbers

Hi, i have a simple control like this: if(sum>1.0)... If i try to print sum i get 1.000000 but the check returns true. I think it depends on float precision. How can i modify the check? thanks (1 Reply)
Discussion started by: littleboyblu
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then compare numbers!

Hi All, I have a file that I need to be able to find a pattern match on a line, take the number on that line check if its >0.9 or <0.1 and if this is true write the line to output.out file. An example of 4 lines in my file is: 1. driver.I177.I11.net010 1.48622200477273e-05 2.... (2 Replies)
Discussion started by: Crypto
2 Replies
Login or Register to Ask a Question