NEWBIE! Please help!

B

BDK

I am trying to take a peice of a word and replace it with output of
the chr function. So the word is ki45. I want to replace the 45 with
what "print (chr(36));" returns.

Here is what I have tried:

$word = ki45;
$word =~ s/(45)/(print (chr(chr36))/;
print ($word);
print "\n";

but that returns:
ki(print (chr(chr36))


I have also tried:
$test = print (chr(chr36);
$word = ki45;
$word =~ s/(45)/$test/;
print ($word);
print "\n";

but that returns this:
ki1

I'm assuming the 1 is "true", but I want the output which should be $.

Any help would be greatly appreciated. Thanks.
 
P

Peter Hickman

BDK said:
I am trying to take a peice of a word and replace it with output of
the chr function. So the word is ki45. I want to replace the 45 with
what "print (chr(36));" returns.

Here is what I have tried:

$word = ki45;
$word =~ s/(45)/(print (chr(chr36))/;

Remember print just writes its arguments to stdout, it doesnt return anything
usefull. So the print on the line above is bogus. You are in effect replacing
45 with the return value of print (and print DOES NOT return its arguments, it
just prints them).
print ($word);
print "\n";

but that returns:
ki(print (chr(chr36))


I have also tried:
$test = print (chr(chr36);
$word = ki45;
$word =~ s/(45)/$test/;
print ($word);
print "\n";

Almost but you are using print again, try this:

$test = chr(36);
$word = ki45;
$word =~ s/45/$test/;
print $word;
print "\n";
but that returns this:
ki1

When I said that print returned nothing useful what it does return is true /
1. Which is why the output is 1.

You are overdoing the use of '(' and ')', it will just confuse you, besides
'(' and ')' actually have some meaning in a regex.
 
T

tom

bdknoll said:
I am trying to take a peice of a word and replace it with output of
the chr function. So the word is ki45. I want to replace the 45 with
what "print (chr(36));" returns.

Here is what I have tried:

$word = ki45;
$word =~ s/(45)/(print (chr(chr36))/;
..
Use sprintf for the above:

$word =~ s/(45)/sprintf ("%s",chr(36))/e;


Tom
ztml.com
 
G

gibbering poster

BDK said:
I am trying to take a peice of a word and replace it with output of
the chr function. So the word is ki45. I want to replace the 45 with
what "print (chr(36));" returns.

Here is what I have tried:

$word = ki45;
$word =~ s/(45)/(print (chr(chr36))/;
print ($word);
print "\n";

but that returns:
ki(print (chr(chr36))

Try using the e mod which executes the right-hand side of a
substitution:

untested:

$_=ki45;
s/\d+/chr$&/e and print

A couple notes:

1) Best to keep scalar strings in single quotes
2) If you're not going to be matching character 45 every time, you need
a more dynamic approach to your regex... possibly:
s/ki(\d+)/"ki".chr$1/e

To replace all number sequences in a string with their corresponding
character value:

$word =~ s/\d+/chr $&/ge;
 
U

Uri Guttman

gp> untested:

gp> $_=ki45;

why assign to $_? just bind to the var.

gp> s/\d+/chr$&/e and print

don't use $&. grab the matched string and use $1. using $& anywhere will
slow down all the s/// operations in your entire program.

uri
 
T

tom

anno4000 said:
Why? How does "sprintf()" improve the value of "chr(36)"?

Anno

It would not on the example. I was simply pointing out that the function
sprintf should be used instead of print - print returns true or false whereas
sprintf returns the value, which that was what the example need.

Tom
ztml.com
 
T

Tad McClellan

It would not on the example. I was simply pointing out that the function
sprintf should be used instead of print - print returns true or false whereas
sprintf returns the value, which that was what the example need.


chr() returns the value too, so there is no need to pass it
through any other function first.


$word =~ s/(45)/ chr(36) /e;
 
B

BDK

Thanks everyone. I couldn't wait for my original email to be posted,
but I eventually figured it out. I did this:

$word = ki45;
$word =~ s/45/\x24/;
print ($word, "\n");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help for a newbie 13
Newbie... 2
Can't solve problems! please Help 0
Please help 7
Hello and Help please :-) 1
Translater + module + tkinter 1
Code help please 4
Help in hangman game 1

Members online

Forum statistics

Threads
473,808
Messages
2,569,686
Members
45,454
Latest member
FionaValli

Latest Threads

Top