perl: string comparison as numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users perl: string comparison as numbers
# 8  
Old 05-27-2007
Quote:
Originally Posted by jim mcnamara
You've all pointed out why it doesn't work but not answered the implied question:
Unfortunately I don't see this as an implied question, because Perl is very different from C, although Perl is itself written in C, and that's all it is to it.

Quote:
Originally Posted by jim mcnamara
How to create the ascii numeric value for characters? Or more likely an array of numbers base 10 from a string.
This is not needed in most scenarios. Perl has regular expressions, so we do not need to compare byte by byte to search for something. Also, Perl supposedly acts on units of "characters", not "bytes". If you take multibyte characters such as UTF-8 into account (which Perl supports), a character may yield more than 1 byte, so individual bytes as a unit no longer makes any sense. So, for known strings, creating an array of bytes is IMO an evil, and being one of the reasons why many Perl programs are not multibyte safe. Of course, for a binary scalar, the situation is different.

Why set up a loop to do it? A one-liner will do:

@bytes = map { ord } split(//, $string);

but I really doubt how useful such an array is to me.

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

But Perl is not C. Perl people rarely, if ever, does anything like that, at least in common scenarios. I have been writing Perl for nearly 7 years and I have not yet encountered a single scenario that strictly necessitates ord() or anything like that.

Learning a language, you cannot quite expect to translate your past habit with other languages and expect it is still applicable. That's probably I have personally reviewed some other's code in the past that Java looks more like C, Perl looks more like sh/sed or BASIC, or highly sloppy PHP with mixed HTML, Javascript and other monsters. If using numeric comparison is wrong for strings in Perl, then it is wrong. This is language design, and it is not quite science so there is not quite an explanation for it. So one cannot rationalize an anomaly by masking it as something else and pretend as though it is working.
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