I need the perl solution for the following :
$string="I LOVE INDIA"
now, in a new string i need the first character of each word...
that is string2 should be "ILN".
I need the perl solution for the following :
$string="I LOVE INDIA"
now, in a new string i need the first character of each word...
that is string2 should be "ILN".
ILN or ILI?
% perl -E'say shift=~/\b\w/g' 'I LOVE INDIA'
ILI
For older versions:
perl -le'print shift=~/\b\w/g' 'I LOVE INDIA'
ILI
ILN is gettin printed on the console....I dont need that
String1 has "I LOVE INDIA"
I want the operation to be performed on string1(the variable) and at the end of it string2 should have ILN
$acr="I LOVE INDIA";
$acr=~/\b\w/g;
print $acr;
This did not work.....
do it step by step
1) split the string up with delimiter as space -> split /\s+/ , $string
2) loop through the array using a for loop
3) get the first character using substr(). do an uc() on it if desired.
$acr = 'I LOVE INDIA';
$acr1 = join '', $acr =~ /\b\w/g;
print $acr1;
I'm not a Perl monk (just learning for now), but I believe that the match operator =~ does not modify the left operand $acr.
The whole expression $acr=~/\b\w/g does return a value, scalar value or list value depending on the context.
$does_it_match = $acr=~/\b\w/g
@all_matches = $acr=~/\b\w/g
Since print provides an array context, if you do
print $acr=~/\b\w/g
then all matches are sent to stdout.
Thanks everyone, All your replies have been really helpful!
Just for ref awk solution
echo "I LOVE INDIA"|awk 'BEGIN{RS=" "}{printf "%s",substr($0,1,1)}'
Radouluv,
Can u explain this to me....
$acr = 'I LOVE INDIA';
$acr1 = join '', $acr =~ /\b\w/g;
print $acr1;
$acr =~ /\b\w/g;
The regular expression \b\w matches every (because of the g option) "word" character (alphanumeric plus "_") at the beginning of the word (\b - word boundary). The above expression $var =~ /regex/ in list context (implied by the join function) returns a list consisting of the matched string(s): ILI. The join function joins the separate strings of that list into a single string with fields separated by the value of its first argument (the empty string in our case), and returns that new string which is assigned to the scalar variable $arc1.
$str="Do you Love me? If so,pls kiss me!";
my @arr=$str=~/\b[a-zA-Z]/g;
print join ",", @arr;