Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

masking string 4

Status
Not open for further replies.

josoers

Programmer
May 13, 2003
17
0
0
NL
i have written the following PHP function that masks a string with ***'s and then unveils 1/3 of its original letters to form a hint:

$length = strlen($answer);
$hint = "";
for ($x=0;$x<$length;$x++){
$hint .= "*";
}
for ($x=0;$x<($length/3);$x++){
$ran = rand(0,$length-1);
$hint[$ran] = $answer[$ran];


Now I need this in perl. I tried this:

$length = length($answer);
$lower=0;
$upper=$length;
$hint = "";
for ($x=0;$x<$length;$x++){
$hint .= "*";
}
for ($x=0;$x<($lengte/3);$x++){
$ran = int(rand( $upper-$lower+1 ) ) + $lower;
substr($hint, $ran, $ran) = substr($antwoord, $ran, $ran);

the masking to **** works but the unveiling doesnt work properly. Now I know nothing about perl so most probably theres an easy and correct way, could someone show me?

thanks
 
($lengte/3) <- could this be the problem. Also, please use
Code:
 tags when posting code, thanks

--Paul

cigless ...
 
your random number generator is wrong also - only one argument required


Kind Regards
Duncan
 
oops no sorry I forgot to rename that one for this post, I changed the variables to english for better reading. So that one should be $length
 
i have to be honest...

substr($hint, $ran, $ran) = substr($antwoord, $ran, $ran);

i have not got a clue what this means!?


Kind Regards
Duncan
 
the generator works, it does have one argument. I used that in another chunk of code as well
 
well thats just my lack of knowledge of perl... ;)

in PHP i have this

$hint[$ran] = $answer[$ran];

this replaces the $ran'th element of the mask string with the $ran'th element of the original string, showing part of the original string in the mask string.

i thought i could do this in perl as well.. but i think it is wrong yes..
 
josoers

try running a portion of your script:-

Code:
#!/usr/bin/perl

$length = 10;

$ran = rand(0,$length-1);


Kind Regards
Duncan
 
$antwoord should be read as $answer.. forgot to rename that one too
 
josoers

i'm not having a go at you - i'm just trying to tell you that this is not valid Perl syntax - or i am too pissed to understand the script!


Kind Regards
Duncan
 
please tell me what you are trying to achieve with this line:-

substr($hint, $ran, $ran) = substr($antwoord, $ran, $ran);


Kind Regards
Duncan
 
i.e. you have no offset or length parameters so why bother using substr?

you are not aiming to get a part of a string - so substr seems a bizarre choice to me? in fact it is so bizarre i am wondering if i am missing the point...


Kind Regards
Duncan
 
say i have $answer = 'apple'

what i want to achieve is getting the string masked and show only 1/3 of the original letters and place this in $hint

So $hint could be *p*** or **p*e

It seemed to me that when $ran was 2 for example, this line

substr($hint, $ran, $ran) = substr($answer, $ran, $ran);

would take the second letter of $answer and replace the * in $hint with that letter. Similar to
$hint[$ran] = $answer[$ran]; in PHP

 
i am quite pissed - sorry for that

if you removed the $hint & $answer you would be left with

Code:
substr($ran, $ran) = substr($ran, $ran);

what is that going to achieve?


Kind Regards
Duncan
 
haha it's ok dude! Im not asking to validate this code as it probably is crap. Im not into perl. What im asking is a correct and easy way to achieve the masking goal.

In my vision substr($x, $ran, $ran) would be the $ran'th letter of the string $x?
 
which makes me think. It should be substr($x, $ran, 1) then
 
yeah that was the problem.. got it sorted now, thanks anyway!

Code:
$length = length($answer);
$lower= 0; 
$upper= $length; 
$hint = "";
for ($x=0;$x<$length;$x++){
$hint .= "*";
}
for ($x=0;$x<($length/3);$x++){
$ran = int(rand( $upper-$lower+1 ) ) + $lower; 
substr($hint, $ran, 1) = substr($answer, $ran, 1);
}
 
a different way of doing the same thing:

Code:
my $word = 'scientific';
my @word = split(//,$word);
my @hint = ('*') x scalar(@word);
for (0 .. scalar(@word)/3) {
   my $index = int(rand @word);
   redo if $hint[$index] ne '*';
   $hint[$index] = $word[$index];
}
print join('',@hint);
 
works a little better like this:

Code:
my $word = 'scientific';
my @word = split(//,$word);
my @hint = ('*') x scalar(@word);
for (0 .. [b](scalar(@word)/3)-1[/b]) {
   my $index = int(rand @word);
   redo if $hint[$index] ne '*';
   $hint[$index] = $word[$index];
}
print join('',@hint);
[code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top