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:
[SOLVED] 2 beginner questions

 



HaHaMan
New User

Aug 16, 2012, 9:26 AM

Post #1 of 7 (1362 views)
[SOLVED] 2 beginner questions Can't Post

First of all, sorry for my english ;)

I got two questions:

1) I have a variable which contains a hexadecimal number, for example:
3e5c4685da

I want to print it as '3e 5c 46 85 da'. In other words, to use a space separator every two hexadecimal numbers. How can I do that?




2) I was trying to use Regular Expressions but I still get a null variable. I got a string like this:
'%SystemDrive%\Documents and Settings\XXXXX'

I want to save the 'XXXXX' in a variable, and I know that is always after the last backslash (\).

I tried this:

Code
$owner = %SystemDrive%\Documents and Settings\LocalService; 
$owner =~ tr/[\\.*$]//;


But when I print it, $owner contains no data. Any solution?



Thanks!!


(This post was edited by HaHaMan on Aug 16, 2012, 12:35 PM)


FishMonger
Veteran / Moderator

Aug 16, 2012, 9:56 AM

Post #2 of 7 (1356 views)
Re: [HaHaMan] 2 beginner questions [In reply to] Can't Post

1)

Code
my $hex = '3e5c4685da'; 
$hex = join(' ', $hex =~ /(..)/g);

print $hex, $/;


2)

Code
use File::Basename; 

my $path = '%SystemDrive%\Documents and Settings\XXXXX';
my $file = basename($path);

print $file, $/;



Laurent_R
Veteran / Moderator

Aug 16, 2012, 10:00 AM

Post #3 of 7 (1355 views)
Re: [HaHaMan] 2 beginner questions [In reply to] Can't Post

tr// does not deal with regular expressions and will not lead you anywhere near where you want to go.


Code
my $XXX = $1 if $line =~ m{([^\\]+)$};


For the other on e, I'll come back later, it is a bit more difficult than you think.


(This post was edited by Laurent_R on Aug 16, 2012, 10:15 AM)


Laurent_R
Veteran / Moderator

Aug 16, 2012, 10:24 AM

Post #4 of 7 (1351 views)
Re: [HaHaMan] 2 beginner questions [In reply to] Can't Post

Well contrary to what I said just earlier, the hex one is also fairly easy. I was thinking of a similar problem, separating the thousands by a comma in a number (e.g. to obtain 56,786,564), which turns out to be a bit tricky.

Here, a simple regex like this one will do it.


Code
my $hex_nr = "'3e5c4685da'; 
$hex_nr = =~ s/(.{2})/\1 /g;



HaHaMan
New User

Aug 16, 2012, 12:39 PM

Post #5 of 7 (1342 views)
Re: [HaHaMan] [SOLVED] 2 beginner questions [In reply to] Can't Post

Thanks guys, finally got my script working ;)

@Lauren
I was traying to do the same as this page says:
http://www.troubleshooters.com/codecorn/littperl/perlreg.htm#DoingTranslations

Maybe it is outdated or I did wrong


Laurent_R
Veteran / Moderator

Aug 16, 2012, 1:47 PM

Post #6 of 7 (1338 views)
Re: [HaHaMan] [SOLVED] 2 beginner questions [In reply to] Can't Post

I think you understood it wrong.

The tr// function is powerful, but is not doing regexes, but simple character substitution.


FishMonger
Veteran / Moderator

Aug 16, 2012, 1:51 PM

Post #7 of 7 (1335 views)
Re: [HaHaMan] [SOLVED] 2 beginner questions [In reply to] Can't Post

The example given in that "tutorial" was wrong and misleading.

 
 


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

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