Hi All,I am new to Perl and I have received a request from client. Situation is, in DCT, I have field named "Question". Now I have to pick first two character of each word in question to populate it QuestionID. If there is single character in question string say "a minute to go?" then Question id should be "amitogo". What regex or perl script will work in this concept. Your valuable suggestion will be appreciated.Regards,Ace
my $string = "a minute to go?";my $newstring = "";foreach (split(/[\s\W]/, $string)) { $newstring .= (/^(\w\w?).*$/) ? $1 : "";}print "$newstring\n";
my $string = "a minute to go?";(my $x = $string) =~ s|(\w\w?)[^\s]*\s*|$1$2|g;print "$x\n";
This also seems to work:my $string = "a minute to go?";(my $x = $string) =~ s|(\w\w?)[^\s]*\s*|$1$2|g;print "$x\n";but it generates a bunch of warning messages.
Remove $2, it is not needed. "Use of uninitialized value..." warnings will disapper along with it. Regex class negation is also a bit of the overkill. So:(my $x = $string) =~ s|(\w\w?)\S*\s*|$1|g;