Im looking for some suggestions on some code I just wrote. I have a friend who works in the real estate business and he needed some help importing some data into a microsoft excel database. A friend or business associate sent him a rather large text file with contacts. He wasnt sure what file the program was from and didnt know how to import it. He only needed the name and address for each record. I told him to send me the file and I would see what I could do.

Looked like a good chance to see if I could write some usefull code. I have to say I was impressed how easy it was to get something working with my limited perl knowledge and in such short time (about 25 minutes). Now Im looking for suggestions on my code. Since I have no idea how to use OLE services I tried out the Spreadsheet::WriteExcel module.


Sample line of data from text file

"1909","W","22th St ","Santa Ana CA","92688-2328", "C002","Jose Z Cuerva","Jose","Z","Cuerva","","","","", "","","1909 W 22th St","Santa Ana CA","92688-2328", "","(714)555-1212","Single Fam Res","SA","974","5", "3","1.0","","","1","$137,500","F","09/09/1999", "456645","","$108,573","25","$199,999",".15A","6,352", "022-022-22","1922","Oceanside MTG","$199,999","","VAR","FHA"



My Code

#!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my ($inp_line, $num_addr, $dir_addr, $str_addr, $city_addr, $zip_addr, + $junk, $name, $junk2) = ''; my $inp_file = shift @ARGV || die "\nusage: $0 inpfile\n"; open (INP, "$inp_file") or die "\nCan't open file [$inp_file]: $!\n"; my $workbook = Spreadsheet::WriteExcel->new("$inp_file.xls"); my $worksheet = $workbook->addworksheet(); $worksheet->write(0,0,"NAME"); $worksheet->write(0,1,"ADDRESS"); $worksheet->write(0,2,"CITY"); $worksheet->write(0,3,"ZIP"); my $row = 2; my $temp = ''; while (<INP>) { chomp; ($num_addr,$dir_addr,$str_addr,$city_addr,$zip_addr,$junk,$name,$j +unk2) = split /\",\"/; $num_addr =~ /^\"(.*)$/; $num_addr = $1; $worksheet->write($row,0,$name); if ($dir_addr){ ($temp = $num_addr . " $dir_addr" . " $str_addr") }else { ($temp = $num_addr . " $str_addr"); } $worksheet->write($row,1,$temp); $worksheet->write($row,2,$city_addr); $worksheet->write($row,3,$zip_addr); ++$row; } close (INP);

Thanks!!
zzspectrez



In reply to Creating Excel Spreadsheet from text data file by zzspectrez

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.