Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Adding formatting to a string

by Clownburner (Monk)
on Jun 16, 2004 at 01:29 UTC ( #367097=perlquestion: print w/replies, xml ) Need Help??

Clownburner has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Adding formatting to a string
by Sidhekin (Priest) on Jun 16, 2004 at 01:57 UTC

    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: Adding formatting to a string
by Not_a_Number (Prior) on Jun 16, 2004 at 07:38 UTC

    You could simplify your own snippet somewhat:

    $string = join '-', unpack 'A4 A*', $string;
Re: Adding formatting to a string
by ambrus (Abbot) on Jun 16, 2004 at 09:17 UTC
Re: Adding formatting to a string
by Anonymous Monk on Jun 16, 2004 at 10:31 UTC
Re: Adding formatting to a string
by injunjoel (Priest) on Jun 17, 2004 at 17:07 UTC
    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
Re: Adding formatting to a string
by melora (Scribe) on Jun 17, 2004 at 16:47 UTC
    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
by Anonymous Monk on Jun 16, 2004 at 11:18 UTC
    You could do this:
    $string = sprintf("%4s-%4s", $string);
    or this:
    $string = join('-', (split($string))[0..4], (split($string))[0..4]);
      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.
        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"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://367097]
Approved by FoxtrotUniform
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2021-12-08 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?