Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Alignment of text using regex

by imp (Priest)
on Jan 16, 2007 at 18:13 UTC ( #594947=note: print w/ replies, xml ) Need Help??


in reply to Alignment of text using regex

While you can do this with a regex it wouldn't be as easy to maintain as an unrolled version using split and printf.

Contrast this:

use strict; use warnings; $/ = undef; my $data = <DATA>; $data =~ s{^(.*?)\|}{sprintf '%-45s', $1}meg; print $data; __DATA__ ABCDEFGHIJKLMNOPQRST|1234 ABCDEFGHI|12345 ABCDE|1234 ABCDEFGHIJKL|123 ABCDEFG|123456 ABCDEFGHIJKLMNO|12
With:
use strict; use warnings; while (my $line = <DATA>) { my ($lhs,$rhs) = split /\|/, $line, 2; printf "%-45s%s", $lhs,$rhs; } __DATA__ ABCDEFGHIJKLMNOPQRST|1234 ABCDEFGHI|12345 ABCDE|1234 ABCDEFGHIJKL|123 ABCDEFG|123456 ABCDEFGHIJKLMNO|12


Comment on Re: Alignment of text using regex
Select or Download Code
Re^2: Alignment of text using regex
by jdporter (Canon) on Jan 16, 2007 at 18:38 UTC

    How about the best of both worlds?

    while(<DATA>) { s(\|){ ' ' x (45-pos) }e; print }
    A word spoken in Mind will reach its own level, in the objective world, by its own weight

      Your dark arts frigten me.

      -Paul

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://594947]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-07-23 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (131 votes), past polls