Re: interpoliation within regexp



Derek B. Smith wrote:
I need to substitute a conversion using chr, but have
failed on multiple attempts. Basically if the first
element contains a # then convert it.

I think you meant "if the first element starts with a digit"? A # is
quite different. Please be as precise as possible.

Will anyone advise?

#if first char is a-z then print it else warn

This comment does not match the code. You are not checking for a-z,
you're checking for non-digits. That includes underscores, whitespace,
capital letters, punctuation, and non-printable characters.

#chop string into individual characters

my @chars = unpack
("A1" x length($password),$password);

if ($chars[0] =~ /^\D/) {
print "Your new string is:\t",@chars,"\n";
}
else {
print "string starts with number, now converting\n", @chars, "\n";
substr($chars[0],0,1) =~ s/\Q{$chars[0]}/{chr($chars[0]}\E/;

what's with all the brackets? Are you aware that you're specifically
searching for actual { and } characters?

Are you aware that the regexp ends at the second /, and that the \E
therefore is meaningless in the replacement?

Are you aware that the replacement is a double-quoted string, not Perl
code, unless you add the /e modifier?

I also have no idea why you're using substr. If you want to only
change one character, change only one character.

$chars[0] =~ s/^(\d)/chr $1/e;

Hope this helps,
Paul Lalli

.



Relevant Pages

  • Re: Simple Array question - Newbie
    ... missing is that it is outputing all characters until it finds a chr, ... > Then, referring to the array as greeting only from the code, will ... > imply the address of the first element in the array. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: (first hello) -> *** Error: unbound variable: first
    ... to meaningfully extract a first element from a word, ... word as a sequence of characters, and a character cannot itself be a ... so characters are a subset of words. ...
    (comp.lang.scheme)
  • Re: PIC P put to real use
    ... PICTURE and the INSPECT in such a case, because it's clearer in both ... characters being replaced as well as the replacement text! ... about the replacement of characters in a numeric-edited data item once its ... I actually like that better than the reference modification. ...
    (comp.lang.cobol)
  • Re: PIC P put to real use
    ... PICTURE and the INSPECT in such a case, because it's clearer in both ... characters being replaced as well as the replacement text! ... about the replacement of characters in a numeric-edited data item once its ... I actually like that better than the reference modification. ...
    (comp.lang.cobol)
  • Re: Need Cyrillic Font with Accents
    ... I found a replacement solution that is not too bad. ... I guess you mean "Combining Diacritical Marks". ... The idea of using such characters is in full conformance to the Unicode ... that reflects the Unicode idea of implementing combining diacritics). ...
    (comp.fonts)