http://qs321.pair.com?node_id=479348


in reply to Data Salad Address Problem

Fun exercise, here was my approach, I make the assumption that the last block of data is your city_state_zip jumble and the first block is your customer, everything else in the middle is addressing info. Then I pull everything apart from its ends city_state zip then I have to make the horrible assumption that there is a state code... in either case, I can still put city_state together which you can see in the dump if you uncomment it. About as fancy as I care to get in 25 mins. Cheers.
use strict; use Data::Dumper; my @customers; while(<DATA>){ my %customer; my (@data) = split /[\t]/, $_; my $city_state_zip; my $which = 0; for( 1..$#data){ $which = $#data -$_; $city_state_zip = $data[-$_]; last if ( $city_state_zip =~/\w+/ig ); } $customer{name} = $data[0]; for( 1..5){ if( $_ <= $which ){ $customer{qq/address$_/} = $data[$_]; } else { $customer{qq/address$_/} = "NULL"; } } $customer{city_state_zip} = $city_state_zip; push @customers, \%customer; } foreach my $c ( @customers ){ my ($city_state, $zip ) = ($1, $2, $3) if $c->{city_state_zip} =~/(.*)\s+([\d\-]+)/g; my ($city, $state) = ($`, $1) if $city_state =~/\s(\w+)$/; $c->{city} = $city; $c->{state} = $state; $c->{zip} = $zip; print " $c->{name}$c->{address1} $c->{address2} $c->{address3} $city, $state $zip\n"; } #print Dumper(\$customers[0]); __DATA__ BIGGLE EQUITY MGMT ASSOC. PIGGLE BIGGLING PLAN FUTURES I & II + TWO LONDDDD CENTER TRENTON NJ 97302-5115 AGRICULTURAL LIFE INSURANCE CO MAIN 111 ELEVEN AVE PO BOX 123 + MORRISTOWN NJ 97631-0633 MILLENA BANC MILLINER BANK & TRUST CO 456 THAUMATURGE AVENUE + NEWSOME MA 07145-6316 BRICKABRACK ADVISORS LIBERTARIAN BANK 888 CRAZED CREEK ROAD ST +E 11 MEMPHIS KY 77882-5394 FOO COMMUNITY BANCORP THE FOOVILLE SAVINGS BANK 18 FOOL AVENUE + FOO NY 10567-1735 ELROND ASSET MANAGEMENT BRA3255 CSHYTFD ATTN SETTLEMENTS DEPT. + 466 LURCHING AVENUE NEW YORK NY 10347 CRIPPLE CREEK INVESTMENT ADVISORS GLIB TALKER 666 9TH AVENUE 8 +TH FLOOR NEW YORK NY 10235-2302 SEPARATIST STATE BANK ATTN: JOSE KWAN XYZ FINANCIAL CENTER + 145 TALKING FAST ROAD NEWBURGH NY 07083-2340

I wanted to add that, I think the way Perl allows you to develop solutions to real world problems like this so quickly and elegantly speaks volumes of praise for the mountain of programmers that built this language. I don't think I have seen a closer knit community of folks so willing to help other programmers learn and develop than right here on Perl Monks either. :0) Super!
JamesNC