Article: 1838 of comp.lang.perl Xref: feenix.metronet.com comp.lang.perl:1838 Path: feenix.metronet.com!news.utdallas.edu!hermes.chpc.utexas.edu!cs.utexas.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!noc.near.net!uunet!mcsun!Germany.EU.net!pki-nbg.philips.de!pki-nbg.philips.de!ln_smr From: Stephen Riehm Newsgroups: comp.lang.perl #Subject: Re: Line wrapping, why do I find this so difficult? Date: 26 Mar 93 09:53:32 GMT Organization: Philips KOmmunikations Industrie AG Lines: 82 Message-ID: References: Reply-To: ln_smr@pki-nbg.philips.de NNTP-Posting-Host: hitkw14.pki-nbg.philips.de Stephen Riehm writes: >Hi guys 'n' gals, > I am trying to wrap lines longer than the screen width, word > breaks don't interest me, but tabs do! etc etc etc... Thanks to the millions of people who replied, the number and range of replies was simply overwhelming! To sum up what I have found to be the most elegant solutions I received the following: --- # From: cameron@cs.unsw.oz.au # detab: convert tabs to spaces sub detab # (tabbed,tabsize) -> untabbed { local($line,$tabsize)=@_; local($_,$chunk); defined($tabsize) || ($tabsize=8); # Bug in regexps? # s/\t/' ' x ($tabsize-(length($`)%$tabsize))/eg; $_=''; for $chunk (split(/\t/,$line)) { $_.=$chunk; $_.=(' ' x ($tabsize-(length($_) % $tabsize))); } s/[ \t]+$//; return $_; } --- Once I got this, it was pretty simple to put together the following while loop: --- while( <> ) { chop; # convert tabs to spaces # If the line is empty, turn it into a single space so that it # gets printed by the while loop below $_ = &detab( $_, $tabsize ) || " "; while( s/(.{1,$width})// && ( ++$line_num < $max_lines ) ) { print "$1\n"; } last if $line_num >= $max_lines; } --- And then, just as I had put my script in /usr/local/bin, I got the following from Eric Arnold: --- I just got a response to my question to the newsgroup about "s///eg ...". It looks like the most concise version is: while ( $test =~ s/\t/" " x (8 - length($`) % 8)/e ){}; I wanted to use the "g" option to "s/", but it doesn't work like I thought. You have to supply the while loop. --- So my problem is solved, and thanks to the people who replied, you are too many to mention, but I am grateful to you all! Thanks guys 'n' gals! -------------------------------------------------------------------- Stephen Riehm Configuration Management _-_|\ ln_smr@pki-nbg.philips.de Philips Kommunikations Industrie / \ Work: +49 911 526 2975 Nu"rnberg, Germany \_.-.!/ Fax: +49 911 526 3678 "I was there, now I am here!" v "My company speaks another language, I CAN'T speak on its behalf" PS: You may think I am joking, I can't speak the language that well so PLEASE DON'T REPLY IN GERMAN! - thanks -