in reply to Re: Adding formatting to a string
in thread Adding formatting to a string

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.

Replies are listed 'Best First'.
Re^3: Adding formatting to a string
by Anonymous Monk on Jun 16, 2004 at 22:12 UTC
    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"

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

      #!/usr/bin/perl @list = split //, 'ABCDEFGH'; s&&local$/; <DATA>&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!