Greetings Monks, I have a csv file which I'll use Perl to read in and do some manipulations and then output it in a new .csv file. Snippet of the input file (not the first 3 lines of the file). Each line starts with the word "map":
Map,Axxxx,F,Bxxxx,BXXXX Axxxx,000000,000000,XXX-XXXXXXXX-XX,,NOTE:XXXX +X Xxxxx (000000)@EX%SMTP:xxxxx.xxxxx@xx.com%X400:c=CA;a= ;p=XX;o=xxxx +xxxxxxxxx;s=XXXXX;g=Xxxxx;,p:000000@mail,Customer Srv Centre,WORKS/ S +RV,SUPPORT/ SUPPORT,AXD3, + 0000,(000) 000-0000,(000) 000-000,,,666 S +nowball Paves,Vancouver,ON,CAN,J7G 3Y4,/o=O:ou=AR3/000000, Map,...
Here is the code that I used. I am using Perl 5.8 on a Unix machine.
#!/usr/local/bin/perl use strict; use Text::CSV_XS; my ($mday, $mon, $year) = (localtime(time))[3..5]; my $cdate = sprintf "%02d" x 3, $mon+1, $mday, $year%100; my $infile = "inmap.csv"; my $outfile = "outmap${cdate}.csv"; open( INFILE, $infile); open (OUTFILE, ">", $outfile); my $csv = Text::CSV_XS->new({binary=>1}); while (defined(my $line = <INFILE>)) { next if $. == 1; chomp $line; if (my $status = $csv->parse($line)) { my @columns = $csv->fields; # quote columns with commas @columns = quotecolumn(@columns); #get email address for (9) { $columns[$_] =~ s/^.*SMTP://; $columns[$_] =~ s/\%.*$//; $columns[$_] =~ s/\".*$//; } #get street number and name for (20) { $columns[$_] =~ s/,.*$//; } print OUTFILE join(",", @columns[1,2,3,4,5,9,11,12,13,15,16,17,18, +19,20,21,22,23,24]), "\n"; } } close (INFILE); close (OUTFILE); sub quotecolumn { my @columns = @_; for (@columns) { if (/,/) { unless (/^"[^"]*"$/) { $_ = qq("$_"); } } } return @columns; }
Everything works except the getting the street number and name part. For example: for the first line, I would like the Address column to only contain 666 Snowball Paves and not 666 Snowball Paves,Vancouver,ON,CAN,J7G 3Y4. When I run the above code, it doesn't give me only the street number and name, it gives me everything. I'm a beginner in Perl and any help would be appreciated. Thanks in advance!

20050511 Edited by Corion: Wiped personal information from the post


In reply to parsing a field by hotpower

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.