LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: How do I do this? (http://www.linuxquestions.org/questions/programming-9/perl-how-do-i-do-this-895809/)

yjy4321 08-05-2011 06:54 PM

Perl: How do I do this?
 
Sorry people.. it is python.... not perl... is there a way to change the title?

Hi,

I am new to perl language. I have a string.

I want to get the PBot id number (1D)
It is a hex number. I want to convert it to a decimal number and print it.

Thank you,

Code:

#!/usr/bin/python
data = "iwconfig ath0 essid PBot_1D channel 4955M"

pbot_id =    # I need help on this one...

print "PBot ID is", pbot_id


Proud 08-05-2011 07:58 PM

1) That looks like you're using Python to me
2) What's your attempt so far?
3) Look into regular expressions, pattern matching is probably more reliable than going for a specific static character/substring position.

Sergei Steshenko 08-06-2011 01:30 AM

Quote:

Originally Posted by yjy4321 (Post 4434881)
Hi,

I am new to perl language. I have a string.

I want to get the PBot id number (1D)
It is a hex number. I want to convert it to a decimal number and print it.

Thank you,

Code:

#!/usr/bin/python
data = "iwconfig ath0 essid PBot_1D channel 4955M"

pbot_id =    # I need help on this one...

print "PBot ID is", pbot_id


You start from reading http://perldoc.perl.org - for starters, from finding out what exists in the left column of the web page.

yjy4321 08-06-2011 05:40 PM

Sorry people... It is python...
And the data is all same pattern

data = "iwconfig ath0 essid PBot_18 channel 4950M"
data = "iwconfig ath0 essid PBot_19 channel 4955M"
data = "iwconfig ath0 essid PBot_1A channel 4960M"
data = "iwconfig ath0 essid PBot_1B channel 4965M"
data = "iwconfig ath0 essid PBot_23 channel 4940M"

only PBot id number and channel are changing.

I was looking into parse function, but it doesn't seem to work..

Proud 08-06-2011 07:05 PM

Which parse function? Post your code.

http://docs.python.org/py3k/library/re.html

An example RE pattern for this:
"iwconfig ath0 essid PBot_([1-9]{1}[1-9A-F]{1}) channel 49([0-9]{2})M"

Produces 2 groups, both should be 2 characters in size. It's a start, some assumptions made, you can say if it's too broad/precise.

Or just use slices

http://docs.python.org/py3k/tutorial...n.html#strings

data[25:27] and data[39:41], my indices might be out.

yjy4321 08-08-2011 01:31 PM

Thanks Proud, the following code works!!
Code:

PBot_id = int(data[25:27], 16)
print PBot_id



All times are GMT -5. The time now is 11:44 PM.