[H-GEN] char inset
Byron Ellacott
bje at apnic.net
Tue May 2 20:34:30 EDT 2000
[ Humbug *General* list - semi-serious discussions about Humbug and ]
[ Unix-related topics. Please observe the list's charter. ]
On Wed, 3 May 2000, Grant, Luke wrote:
> "B43" which is the contents of a string variable must become "B043" and i
> have no idea how i can insert a char into a string in Bash or Perl.....and i
> would rather not have to dust of my old "C" texts cause whilst i know that
> strings are treated as nice simple arrays of char in "C" i haven't coded in
> it for over a year and would rather not start now.
A few perl ways:
o $foo =~ s/B43/B043/;
where the regular expression is smarter to cope with your particular
situation.
o $foo = substr($foo,0,1) . "0" . substr($foo,1);
o $foo = join("0", substr($foo, 0, 1), substr($foo, 1));
A bash way:
$ FOO=B43
$ FOO=${FOO:0:1}0${FOO:1}
$ echo $FOO
B043
$
Treat your data as a string, instead of as an array of characters, and it
becomes clearer that you can split the string up and combine it again, in
more ways than the few I've shown here.
--
bje
--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.
More information about the General
mailing list