LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-05-2011, 06:54 PM   #1
yjy4321
Member
 
Registered: Apr 2011
Distribution: Ubuntu
Posts: 42

Rep: Reputation: Disabled
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

Last edited by yjy4321; 08-06-2011 at 05:42 PM. Reason: it is python
 
Old 08-05-2011, 07:58 PM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.
 
Old 08-06-2011, 01:30 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by yjy4321 View Post
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.
 
Old 08-06-2011, 05:40 PM   #4
yjy4321
Member
 
Registered: Apr 2011
Distribution: Ubuntu
Posts: 42

Original Poster
Rep: Reputation: Disabled
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..
 
Old 08-06-2011, 07:05 PM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.

Last edited by Proud; 08-06-2011 at 07:11 PM.
 
Old 08-08-2011, 01:31 PM   #6
yjy4321
Member
 
Registered: Apr 2011
Distribution: Ubuntu
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks Proud, the following code works!!
Code:
PBot_id = int(data[25:27], 16)
print PBot_id
 
  


Reply

Tags
python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 06:59 PM
Trying to find libmail-mbox-messageparser-perl and libmailtools-perl for Fedora 11 almac58 Linux - Software 4 11-18-2009 04:17 AM
LXer: Installing Eclipse, the Epic Perl plugin and my first Perl GUI program LXer Syndicated Linux News 0 05-08-2009 07:41 PM
RHEL4u2 perl localtime() call issues (perl & glibc) Brad.Scalio@noaa.gov Linux - Enterprise 3 01-23-2007 10:27 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM


All times are GMT -5. The time now is 02:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration