Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Extract data from txt file

by davido (Cardinal)
on Nov 30, 2019 at 05:13 UTC ( #11109473=note: print w/replies, xml ) Need Help??


in reply to Extract data from txt file

Well, it's been seven hours since the OP was last seen here, so at this point I can take a crack at it for fun without feeling like I'm doing free work.

#!usr/bin/env perl use strict; use warnings; sub is_street {return shift =~ m/^\d+/;} sub is_postal {return shift =~ m/^\w+.+\d$/;} sub street_components { my $address = shift; if ($address =~ m/^ (.+) \s+APT\s+ # APT anchor (?:\(Range\s+)? # Range syntax ([\w\d]+(?:\s+-\s+[\w\d]+)?) # Apartment number \)? # Closing range syntax $/x ) { return {street => $1, apartment => $2} } else { die "Street address match failure: <<$address>>\n"; } } sub postal_component {return shift} sub apartment_expand { my $apartment_range = shift; my ($low, $high) = split /\s*-\s*/, $apartment_range; return [$low] if !length($high); my ($low_num, $low_alpha ) = $low =~ m/^(\d+)(\w+)$/; my ($high_num, $high_alpha) = $high =~ m/^(\d+)(\w+)$/; my @return; foreach my $num ($low_num .. $high_num) { # Numeric +increment. foreach my $letter ($low_alpha .. $high_alpha) { # Alpha in +crement. push @return, "${num}${letter}"; } } return \@return; } my %record; while (my $line = <DATA>) { chomp $line; next unless length $line; $record{'addr'} = street_components($line) if is_street($line); $record{'postal'} = postal_component($line) if is_postal($line); if (exists $record{'addr'} && exists $record{'postal'}) { my $apartments = apartment_expand($record{'addr'}->{'apartment +'}); foreach my $apartment (@$apartments) { printf "%s, APT %s, %s\n" => $record{'addr'}->{'street'}, $apartment, $record{'postal'}; } undef %record; } } __DATA__ 432 10TH ST APT (Range 2A - 2B) BROOKLYN NY 10598-6601 432 10TH ST APT (Range 3A - 3B) BROOKLYN NY 10598-6601 432 10TH ST APT (Range 4A - 4B) BROOKLYN NY 10598-6605 432 10TH ST APT (Range 5A - 5D) BROOKLYN NY 10598-6605 432 10TH ST APT 6A BROOKLYN NY 10598-6605

This produces the following output:

432 10TH ST, APT 2A, BROOKLYN NY 10598-6601 432 10TH ST, APT 2B, BROOKLYN NY 10598-6601 432 10TH ST, APT 3A, BROOKLYN NY 10598-6601 432 10TH ST, APT 3B, BROOKLYN NY 10598-6601 432 10TH ST, APT 4A, BROOKLYN NY 10598-6605 432 10TH ST, APT 4B, BROOKLYN NY 10598-6605 432 10TH ST, APT 5A, BROOKLYN NY 10598-6605 432 10TH ST, APT 5B, BROOKLYN NY 10598-6605 432 10TH ST, APT 5C, BROOKLYN NY 10598-6605 432 10TH ST, APT 5D, BROOKLYN NY 10598-6605 432 10TH ST, APT 6A, BROOKLYN NY 10598-6605

It's unfortunate that the data lacks a record separator; that means you have to keep track of what state you are in. If it mattered, I'd do more detection of getting out of sync by verifying we didn't get a city before getting an address.

If one were to use this for anything more than amusement they would quickly discover how fragile the address detection is, and that would lead to a realization of how unfortunate the input data format is.


Dave

Replies are listed 'Best First'.
Re^2: Extract data from txt file
by jdporter (Canon) on Dec 02, 2019 at 13:41 UTC
    $/ = ''; $\ = "\n"; $, = ", "; while (1) { local $_ = <> or last; s/\s+$//; my $y = <> or last; $y =~ s/\s+$//; if ( /(.+) (APT \d.*)/ ) { print $1, $2, $y; } elsif ( /(.+) (APT) \(Range (\d+)([A-Z]+) - \3([A-Z]+)\)/ ) { print $1, "$2 $3$_", $y for $4 .. $5; } }
    I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.
Re^2: Extract data from txt file
by karlgoethebier (Monsignor) on Nov 30, 2019 at 10:47 UTC
    "...seven hours..."

    Nice code. But it is like it is:

    "Ihr sollt das Heilige nicht den Hunden geben und eure Perlen sollt ihr nicht vor die Säue werfen, damit die sie nicht zertreten mit ihren Füßen und sich umwenden und euch zerreißen.“ (Matthäus 7,6 LUT)".

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      Same book. Same page even.
      Richtet nicht, auf daß ihr nicht gerichtet werdet. 2Denn mit welcherlei Gericht ihr richtet, werdet ihr gerichtet werden; und mit welcherlei Maß ihr messet, wird euch gemessen werden.
      And
      7Bittet, so wird euch gegeben; suchet, so werdet ihr finden; klopfet an, so wird euch aufgetan. 8Denn wer da bittet, der empfängt; und wer da sucht, der findet; und wer da anklopft, dem wird aufgetan.


      holli

      You can lead your users to water, but alas, you cannot drown them.

        Thanks my fellow-believer 🙏

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2020-06-04 18:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (35 votes). Check out past polls.

    Notices?