Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Data Salad Address Problem

by JamesNC (Chaplain)
on Jul 29, 2005 at 10:59 UTC ( #479348=note: print w/replies, xml ) Need Help??

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!

Replies are listed 'Best First'.
Re^2: Data Salad Address Problem
by SamCG (Hermit) on Jul 29, 2005 at 21:11 UTC
    Wow, I'm truly overwhelmed by the volume and intelligence in the responses! As it is most of the time when I ask a question on PM, the answers will cause me to stretch my knowledge of perl and programming a little more. Thank you all...

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2021-04-14 16:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found