Adding formatting to a string
Clownburner
created: 2004-06-15 21:29:20
Greetings, fellow monks. Is there a better way to do this? I have a string, say "ABCDEFGH" that I need to print as "ABCD-EFGH". My solution, although inelegant, seems to work:
 $string = join('-',unpack('A4 A4',pack('A*',$string)));
There must be a simpler way. Please share with me what it is, as I am unable to figure it out. Thanks in advance.

"Non sequitur. Your facts are un-coordinated." - Nomad
Re: Adding formatting to a string
created: 2004-06-15 21:57:38

Do you want to insert a hyphen in the middle of the string?

substr($string, (length $string)/2, 0) = '-';

... or insert a hyphen after the fourth character?

substr($string, 4, 0) = '-';

... or insert a hyphen after every fourth character, but not at the end?

$string =~ s/(.{4})(?=.)/$1-/g;

... or something else?

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

Re^2: Adding formatting to a string
created: 2004-06-16 03:23:18
Even simpler:
substr($string,4,0,"-")
Re: Adding formatting to a string
created: 2004-06-16 03:38:10

You could simplify your own snippet somewhat:

$string = join '-', unpack 'A4 A*', $string;
Re: Adding formatting to a string
created: 2004-06-16 05:17:21
$string=~s/(....)/$1-/s;
Re: Adding formatting to a string
created: 2004-06-16 06:31:51

$string =~ s/DF/D-F/g;
or more generic
$string =~ s/^(.{4})/$1\-/g;

Re: Adding formatting to a string
created: 2004-06-16 07:18:17
You could do this:
$string = sprintf("%4s-%4s", $string);
or this:
$string = join('-', (split($string))[0..4], (split($string))[0..4]);
Re^2:Adding formatting to a string
created: 2004-06-16 10:53:33
Neither of these is a correct solution. The first will print the entire string followed by a dash, then four spaces. The second will try to split $_ using $string as a regex.

We're not really tightening our belts, it just feels that way because we're getting fatter.
Re^3: Adding formatting to a string
created: 2004-06-16 18:12:38
Oh, right. Oops.
my $string = "ABCDEFGH";
my @string = split //, $string;
$final = (join "", @string[0..3])."-".(join "", @string[4..7]);
print $final; # if my code is right, this is "ABCD-EFGH"
Re^4: Adding formatting to a string
created: 2004-06-17 17:51:11

If you're going to do that you might as well do

#!/usr/bin/perl

@list = split //, 'ABCDEFGH';

s&&local$/;
&e&&y&;
&&d;& s#zoTfFdDsOt##g;
while/(\D)(\d+)/gi;

sub z{(&O-&O)*((&O-&O)*(&O-&O))}
sub o{&z-((&O-&O)*(&O-&O))+&O-&O}
sub T{&z-(&O-&O)}
sub f{&T+(&O-&O)}
sub F{(&O-(&O-&O))/(&O-&O)}
sub d{print chr shift}
sub D(@){map chr,@_}
sub s{print D$1->(@list[split//,$2])}
sub O{ord shift}
sub t{&z+&O-&O(A)}
__DATA__
#t50703010o507040703002o507040703004o507040703003F01020f5070
306057t50703050t50703060o507040703003T50703010T50703040o5070
40703005F01020t50703070T50703040o507040703005t50703030F01020
T50703010f5070306057T50703060t50703020T50703040o507040703005

Proves the same point!

Re: Adding formatting to a string
created: 2004-06-17 12:47:27
How about this?
my $string = "ABCDEFGH";
$string = sprintf("%.4s-%s", $string,substr($string,4,4));
Awkward, maybe, but I have tried it and it works.
Re: Adding formatting to a string
created: 2004-06-17 13:07:43
Greetings all,
Here is my suggested solution:
#!/usr/bin/perl -w
use strict;
my $string = "ABCDEFGH";
print $string."<= before\n\n";
my $regexp = '\w{'.int(length($string)/2).'}';
$string =~ s/($regexp)/$1-/;
print $string."<= after\n\n";

exit;

It outputs:
ABCDEFGH<= before
 
ABCD-EFGH<= after

However this assumes you wanted to split the string in half.
Just a thought

-injunjoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

perlmonks.org content © perlmonks.org and ambrus, Anonymous Monk, blyman, Clownburner, injunjoel, melora, Not_a_Number, Roy Johnson, Sidhekin

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03