Re: substitution



nico io wrote:

I have wrote this :
s/(?<![0-9A-Za-z;])(\d+)(?!(\;\d+|\d+|\;\s+\d+\;\d+))/$hashTable{$1}/g

it works but when a number is not as a key of the hash table it replace
the numer by nothing.

I would like to let the number if it doesn't belong to my hash table,
how can I do ?

Please keep your posts confined to the perl.beginners group so that others can
both help and be helped by your questions.

Please bottom-post your responses to the perl.beginners group so that extended
threads remain comprehensible. Thank you.

You should always put

use strict;
use warnings;

at the start of your program, which would have given you a warning when missing
keys were substituted.

Your regex isn't clearly thought-out. For instance
/(\d+)(?!(\;\d+|\d+|\;\s+\d+\;\d+)/ says that the string can't be followed by a
semicolon and digits, or just digits, or a semicolon and whitespace and digits.
Because (/d+) is greedy and will consume all the digits available there is no
need to specify anything other than /(\d+)(?!;)/.

You can stop missing keys being substituted by making your replacement string an
expression. This should do what you want:

$str =~ s/(?<![A-Z;])(\d+)(?!;)/ exists $hash{$1} ? $hash{$1} : $1 /gei;

HTH,

Rob


.



Relevant Pages

  • Re: convert integer to string
    ... unique key, create a hash element on ... and read the other values into an anonymous hash. ... discovered much to my embarrassment that the keys weren't the same. ... consisted of seven characters which are all digits. ...
    (comp.lang.perl.misc)
  • Re: Suggestion for an AES Based Hash Function
    ... amateurs posting here seem to favour cyphers, I go for hash functions. ... Rather than trying to use a strong one-way compression function would ... I have used a fixed set of keys in two places. ... preferred to have the first round keys fixed and to start deriving ...
    (sci.crypt)
  • Suggestion for an AES Based Hash Function
    ... amateurs posting here seem to favour cyphers, I go for hash functions. ... Rather than trying to use a strong one-way compression function would ... The round function ... I have used a fixed set of keys in two places. ...
    (sci.crypt)
  • Re: sort unique
    ... given that a hash table is not ... IMO if the vendor's algorithm does something "obvious", ... function to eliminate keys that hash to the same bucket per some ... strings of random lengths, and two strings are ...
    (comp.lang.lisp)
  • Re: My First C# (warning - long post)
    ... defined as a COBOL structure of 8192 bytes (this is for ... Hashtables use two objects, one is a 'key' and the other a 'value'. ... but is for storing keys only. ... // Add some elements to the hash table. ...
    (comp.lang.cobol)