steph84
asked on
VERY easy question:
$string="1234123412341234"
I want $string2="1234 1234 1234 1234" from string1 value.
Most efficient/shortly/etc.. way to do that??
I want $string2="1234 1234 1234 1234" from string1 value.
Most efficient/shortly/etc.. way to do that??
$string=~s/41/4 1/g;
#it's not obvious what you might want if
#$string='a ab abcd abcde abcdef abcdefg abcdefgh abcdefghi'
#some possibilities include:
($string2=$string)=~s/(\S{ 4})\B/$1 /g;
#or
$string2=join' ',($string=~/(....)/gs;
#$string='a ab abcd abcde abcdef abcdefg abcdefgh abcdefghi'
#some possibilities include:
($string2=$string)=~s/(\S{
#or
$string2=join' ',($string=~/(....)/gs;
ASKER
In fact, it was obvious...for me!
I just wanted to make group of 4 chars or digits, separated by one blank space.
The last answer is exactly what I was looking for.
you should answer now so I could grade you!!
I just wanted to make group of 4 chars or digits, separated by one blank space.
The last answer is exactly what I was looking for.
you should answer now so I could grade you!!
ASKER
In fact, it was obvious...for me!
I just wanted to make group of 4 chars or digits, separated by one blank space.
The last answer is exactly what I was looking for.
you should answer now so I could grade you!!
I just wanted to make group of 4 chars or digits, separated by one blank space.
The last answer is exactly what I was looking for.
you should answer now so I could grade you!!
$string="1234123412341234"
$string =~ s/4/4 /g;
$string =~ s/4/4 /g;
That puts a space at the end of $string which may not have been wanted.
What if $string contains non-digits, or groups of less than 4 digits separated by spaces?
Should those spaces be removed before regrouping them in fours?
What if $string contains non-digits, or groups of less than 4 digits separated by spaces?
Should those spaces be removed before regrouping them in fours?
ASKER
Sorry hedj, but points are for ozo....
Ozo, no worrying about $string, they'll never have sapce or non digit char.
Thanks for all possibilities, that's what I'm looking for when I ask a question (especially as easy as this one) on experts-exchange..
You should answer!
Ozo, no worrying about $string, they'll never have sapce or non digit char.
Thanks for all possibilities, that's what I'm looking for when I ask a question (especially as easy as this one) on experts-exchange..
You should answer!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.