The phone directory at work is sent out as a Word document (why? I have _no idea_!?). I wrote this little perl script to grab phone numbers for me, it uses Text::Soundex. Due to the fact that I just export the word document to ASCII format, it's got a few quirks in it, that I've had a hard time representing with regex's. So, I ended up using a few .*'s in there and was wondering if some regex guru (Ovid? :^)) could show me the light. That and anyone that would like to take a poke at it, please feel free.

Here's the file format of the phone list
NAME              EXT  RM#    ORG       NAME              EXT  RM#    ORG     
 -------------------------------------   --------------------------------------
 - A -                                   BASILE, YYYY      5555 1H08   IAMG    
 ABEND, YYYYYY     5555 2014   CE        BATES, YYYY       5555 4832   BT      
 ABRAMS, YYYYY     5555 C-07             BATHERSFIELD, YY  5555 B-39   CE      
 ADAMS, YYYY       5555 255    OTC       BAXTER, YYYY      5555 A-43           
 ADAMS, YYYY       5555 149    BT        BEAR, YYYYYY      5555 H42    ATO     
 ADAMS, YYYYYYY    5555 A-16             BEASLEY, YYY      5555 D-79           
 ADUAKA, YYYYYYYY  5555 A-52             BEATTY, YY        5555 4832   TAG     
 AHMED, YYYYYY     5555 C-63             BECHTLE, YYYY     5555 D-26           
 AHMED, C. YYYYYY  5555 D-69   SOMEU     BEDOYA, YYYYYYYY  5555        CE
As you can see, they use things like "- A -" to denote the start of an alphabetical section, which I've had a hard time with. Also, some folks have middle initials, some don't, some have an organization, some don't.
Here's the code:
#!/usr/bin/env perl use strict; use Text::Soundex; my $PHONE_LIST = "$ENV{HOME}/phone_list"; my %seen; my $cntr; if(@ARGV == 0 || $ARGV[0] eq '-?' || $ARGV[0] eq '-h') { print "\nUsage: $0 <last name> ...\n\n"; exit 1; } open(PL, "$PHONE_LIST") || die "Can't open $PHONE_LIST: $!"; while(<PL>) { foreach my $name (@ARGV) { $name =~ /\U$name/; my $code = soundex($name); my $column_one = my $column_two = $_; my $flag; $column_one =~ s/^\s-*\s*(\w+)\s*-*,*.*$/$1/g; $column_two =~ s/^\s\w+,\s.* (\w+),.*/$1/g; if($column_one && soundex($column_one) eq $code) { $flag = 1; } elsif($column_two && soundex($column_two) eq $code) { $flag = 2; } if($flag) { $seen{$name}++; if(++$cntr == 1) { printf("\n%-12s %-15s %-8s %-5s\n", "First", "Last", "Ext", "Rm#"); print "=" x 43, "\n"; } if($flag == 1) { s/^\s(\w+), (\w+)\s+(\w?)\s?(\d{4,10})\s+(\S+)\s*.*$/$ +2 $1 $3 $4 $5/g; } elsif($flag == 2) { s/^\s.*\s(\w+), (\w+)\s+(\w?)\s?(\d{4,10})\s+(\S*)\s+. +*$/$2 $1 $3 $4 $5/g; } my ($fname, $lname, $m, $num, $room) = split(/ /); chomp($room); printf("%-10s %1s %-15s %-8d %-5s\n", $fname, $m, $lname, $num, $room); } } } foreach my $name (@ARGV) { $name =~ /\U$name/; if(! exists($seen{$name})) { print "\nNot found: $name"; } } print "\n"; close(PL);
This is really a quick hack, so it's not as clean as I'd like.

In reply to Need help with regex by BastardOperator

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.