CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
A few basic questions (mostly related to GUIs and parsing)

 



TheMartianGeek
New User

Dec 22, 2010, 9:49 AM

Post #1 of 4 (695 views)
A few basic questions (mostly related to GUIs and parsing) Can't Post

Hello. I'm a novice Perl programmer (if it wasn't clear from where I posted) who has been tinkering around with the language for a couple months on and off and just starting getting into wxPerl. I'm liking Perl quite a bit, but I have a few questions.

1) What's the best way to parse a string as a hexadecimal number and vice-versa? For example, say the user puts in "3C" or "0A7F" as arguments in a command-line program. How do I tell the program to treat those as numbers rather than character sequences? For going the other way around, I assume I can use printf; I've done it that way before, although I'm not sure how well it would work with things like text boxes and labels.

2) Are there any good tutorials for wxPerl? I've searched on Google and all, and I haven't found a one that is actually complete. (I could say the same for GUI programming in other languages as well, such as C++, but this is a Perl forum, after all.) The few I found covered only the first few steps, not to mention they didn't seem to do a very good job of explaining exactly what each element of the program does. I couldn't find much in the way of documentation for the widgets, either. (For instance, I have a program in mind that requires text boxes and checkboxes, and I found no information about either.) Where might I find some good tutorials and documentation?

3) What's the best way to remove any of a certain list of characters from the beginning of a line of text and from the end, or from a certain character onward? It's kind of hard to explain, but I feel like I'm missing something simple here, especially with that subroutine in the FAQ that removes all whitespace from the beginning and end of a string. For example, say I'm parsing a line of assembly code. It may or may not have whitespace characters at the beginning, it may or may not have whitespace characters after the code, and it may or may not have a comment beginning with a semicolon, which could contain any alphanumeric characters. I'd need to disregard everything after any semicolon and any whitespace characters that aren't within the code itself. Is there a way to do that in only, like, one or two steps? I can see it being easily possible with a series of split statements and the like, but when I tried it, it seemed to take more code than I would think would be necessary for such a task in Perl.

If anyone could answer my questions (providing extra information if necessary), I would appreciate it. I will use this thread for more questions in the future when I have them (and believe me, I will).


BillKSmith
Veteran

Dec 22, 2010, 1:22 PM

Post #2 of 4 (688 views)
Re: [TheMartianGeek] A few basic questions (mostly related to GUIs and parsing) [In reply to] Can't Post

I recommend against trying to learn perl and GUI programming at the same time. Use a book such as "Learning Perl" to learn the basics of perl. Your perl installation includes volumes of documentation. At your command prompt, type perldoc perldoc to learn how to access it. This documentation should include all modules installed on your system.

1. Consider the pack and unpack functions for handling hex characters.

2. Sorry, I cannot help with wx. I use Tk.

3. Removing characters is a job for regular expressions. Perl's power with regular expressions is often the reason for choosing perl. Learn the basics and give it a try. Ask us when you have a specific problem.
Good Luck,
Bill


TheMartianGeek
New User

Feb 4, 2011, 6:22 PM

Post #3 of 4 (654 views)
Re: [BillKSmith] A few basic questions (mostly related to GUIs and parsing) [In reply to] Can't Post


In Reply To
I recommend against trying to learn perl and GUI programming at the same time.

I didn't say I was...


In Reply To
1. Consider the pack and unpack functions for handling hex characters.

Hm. I messed with them a little, and I found them kind of hard to understand. The hex and sprintf functions seem to be working fine for everything I'm doing, though.


In Reply To
2. Sorry, I cannot help with wx. I use Tk.

...okay.


In Reply To
3. Removing characters is a job for regular expressions. Perl's power with regular expressions is often the reason for choosing perl. Learn the basics and give it a try. Ask us when you have a specific problem.

Mm-hm. Well, I've already learned a lot of the basics, but I don't really have anywhere to go from here, especially since I own only one Perl programming book.

Also, I have a new question. I have this subroutine:


Code
sub ChangeTilesInPage 
{
$i = 0;
$TileData = 0x0000;
while(!(eof(IN)))
{
$BytesRead = read(IN, $TileData, 2);
unless($BytesRead != 2)
{
$NewTileData = (($TileData & 0xFC7F) | ($GFXFileBits));
print OUT $NewTileData;
next;
}
}
}


It's part of a program intended to read in data from a binary file 2 bytes at a time, AND those two bytes with a certain value (0xFC7F in little-endian), OR them with the value of a variable (set by one of the command-line arguments), and then print the result to a new .bin file (also 2 bytes at a time, of course). I can't figure out the correct way to read in the two bytes, though, and since my OR'd variable is 16 bits, I don't want to read the bytes only one at a time.


BillKSmith
Veteran

Feb 4, 2011, 9:07 PM

Post #4 of 4 (652 views)
Re: [TheMartianGeek] A few basic questions (mostly related to GUIs and parsing) [In reply to] Can't Post

The problem with binary files is getting the bytes, nibbles, and bits all in the right order. I always seem to end up resorting to trial-and-error. Once you know what order they are in, you can unpack them to an array of bits or hex characters, fix the order with an array slice, and then repack. I agree that the documentation for pack and unpack is confusing. I find that the documentation in perldoc -f pack is slightly better than any book that I have. I doubt that reading bytes one at a time would help much at all. It probably is a good idea to examine a sample of your data with a hex editor to verify the order of thing in the file.



Sorry if I was not clear in my previous response (3). By 'learn the basics' I was refering only to the basics of regular expressions.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives