fixed width fields

J

Jim

This should be an easy one but I'm having a brain cramp.

I have a string: 2-s1.0-49FR-WSJ0-TWC1-XXXX-12345-00. I want to
replace the 12345 with $i. When $i equals "1" how can I make it
"00001"?
 
P

peter pilsl

Jim said:
This should be an easy one but I'm having a brain cramp.

I have a string: 2-s1.0-49FR-WSJ0-TWC1-XXXX-12345-00. I want to
replace the 12345 with $i. When $i equals "1" how can I make it
"00001"?

perldoc -f sprintf

example:

$a="2-s1.0-49FR-WSJ0-TWC1-XXXX-12345-00";
$i=234;

$b=sprintf("%05i",$i);
$a=~s/-\d{5}-/-$b-/;

print $a,"\n"

peter
 
V

Vlad Tepes

Jim said:
This should be an easy one but I'm having a brain cramp.

I have a string: 2-s1.0-49FR-WSJ0-TWC1-XXXX-12345-00. I want to
replace the 12345 with $i. When $i equals "1" how can I make it
"00001"?

$str =~ s/12345/sprintf "%05d", $i/e;
 
B

Barry Kimelman

[This followup was posted to comp.lang.perl.misc]

jg_x30 said:
This should be an easy one but I'm having a brain cramp.

I have a string: 2-s1.0-49FR-WSJ0-TWC1-XXXX-12345-00. I want to
replace the 12345 with $i. When $i equals "1" how can I make it
"00001"?

$data = sprintf "%05d",$i;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top