Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Dividing up Long Lines

by moe79 (Initiate)
on Jun 21, 2012 at 16:16 UTC ( #977683=perlquestion: print w/ replies, xml ) Need Help??
moe79 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I need to keep address lines in a file at a maximum of 30 characters. I have a file with these columns:

Address_Line_1 (this is never blank) Address_Line_2 (this could be blank) Address_Line_3 (this could be blank) Address_Line_4 (this is never blank) City (this is never blank) State (this is never blank) Zip (this is never blank)

I am looking to use perl to check if Address_Line_2 is greater than 30 characters.

If it is, I need to divide Address_Line_2 between Address_Line_2 and Address_Line_3, but only if Address_Line_3 is blank.

If I need to divide Address_Line_2, I would like to divide it at a point in where there is a space.

I'm reading the file in like this:
while (<INFILE>) { @fields = split /\t/, $_; $a1 = $fields[0]; $a2 = $fields[1]; $a3 = $fields[2]; $a4 = $fields[3]; $city = $fields[4]; $st = $fields[5]; $zip = $fields[6]; print OUTFILE pack ("A30", $a1); print OUTFILE $TAB; print OUTFILE pack ("A30", $a2); print OUTFILE $TAB; print OUTFILE pack ("A30", $a3); print OUTFILE $TAB; print OUTFILE pack ("A30", $a4); print OUTFILE $TAB; print OUTFILE pack ("A16", $city); print OUTFILE $TAB; print OUTFILE pack ("A2", $st); print OUTFILE $TAB; print OUTFILE pack ("A10", $zip); print OUTFILE "\r\n"; }
Many Thanks!

Comment on Dividing up Long Lines
Select or Download Code
Re: Dividing up Long Lines
by Riales (Hermit) on Jun 21, 2012 at 16:47 UTC

    Looks like you have a good start so far. Doesn't look like you have inserted any logic for dealing with if length($a2) > 30 yet. Maybe try doing that first? Did you run into a problem when you tried it?

Re: Dividing up Long Lines
by toolic (Chancellor) on Jun 21, 2012 at 16:47 UTC
    Text::Wrap might come in handy:
    use warnings; use strict; use Data::Dumper; use Text::Wrap; $Text::Wrap::columns = 30; my $col = 'very long string right here ya know'; my @cols = split /\n/, wrap('', '', $col); print Dumper(\@cols); __END__ $VAR1 = [ 'very long string right here', 'ya know' ];
    It depends if your address 2 can be 60 or longer, or if it is 30 or more but has no spaces. Otherwise, these may help:

      TheDamians Text::Autoformat is also interesting :)

      $ perl -MText::Autoformat -e " autoformat( {qw/ right 30 all 1 /})" - very long string right here ya know ^Z very long string right here ya know $ perl -MText::Autoformat -e " autoformat( {qw/ right 28 all 1 /})" - very long string right here ya know ^Z very long string right here ya know $ perl -MText::Autoformat -e " autoformat( {qw/ left 2 right 30 all 1 + /})" - very long string right here ya know ^Z very long string right here ya know $ perl -MText::Autoformat -e " autoformat( {qw/ left 2 right 30 all 1 + /})" - Address_Line_1 (this is never blank) Address_Line_2 (this could be blank) Address_Line_3 (this could be blank) Address_Line_4 (this is never blank) ^Z Address_Line_1 (this is never blank) Address_Line_2 (this could be blank) Address_Line_3 (this could be blank) Address_Line_4 (this is never blank)

      As is Perl6::Form

      $ perl -MPerl6::Form -ne " print form q{{[[[{ 30 }[[[[}} , $_" - very long string right here ya know very long string right here ya know ^Z

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-07-31 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (246 votes), past polls