Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This is the second Perl script I've done from scratch and I'm havin' a ball! I'd like some constructive criticism over all and in two specific areas.

The objective is to convert an MS Word document of the company phone list to an MS Excel spreadsheet for sorting and conversion to several web pages (of differing sort orders).

The Word file is saved to a text file where each person's info is spread over 3 lines. The script pulls everything into one line per person with tabs separating each item. Having been hand entered, the data is somewhat "dirty". Most of the regexp work is cleaning linefeeds, carriage returns and the random non word characters around the valid data.

Question 1: Is there a more perlish way to count the three lines per person? (What I'm doing looks so much like C.)

Question 2: If you look at the input data for "Bo Jo Le Much" you'll see there is no space after the comma separator and there is a CR (^M) after the first name in the output. If I insert a space after the comma in the input data, the CR in the output goes away. Why?

#!/opt/bin/perl #perl 5.8.3 built for sun4-solaris-64int-ld use warnings; use strict; my $T = "\t"; my $fname = "U1"; my $lname = "U2"; my $phone = "U3"; my $room = "U4"; my $bldg = "U5"; my $email = "U6"; my $temp = "Ut"; my $count = 0; print "\nLast First Phone Bldg Room email\n"; open FH => "<testdata.txt" or die "can't find the data file: $!\n"; #open FH => "<phonedata.txt" or die "can't find the data file: $!\n"; while (<FH>) { chomp; $count++; if ($count == 1){ ($lname, $fname) = split(","); $fname =~ s/^\s+(.+?)\W*$/$1/; }elsif ($count == 2){ ($phone, $temp) = split("/"); #line 28 if ( ($temp =~ m[^\d.*]) || ($temp =~ m[--- --]) ){ ($room, $bldg) = split(" ", $temp); } else { $bldg = $temp; $bldg =~ s/^(.*?)\W*$/$1/; #$bldg includes a 'cr' $room = ""; #no room number, just bldg name } }elsif ($count == 3){ $email = $_; $email =~ s/^(.*?)\W*$/$1/; $count = 0; pA(); }else{ print "Got an overflow - $count $_\n"; } } print "\n" if ($count == 0); close FH; sub pA{print "$lname$T$fname$T$phone$T$bldg$T$room$T$email\n";} ------------------------------------------------------- __Input__ Alanon, Bart 5590/EL ---- O'Lewis, John. ----/--- -- john Le Much,Bo Jo 3406/165 NS ed@a.nl Abe-Jen, Mar-Jo 3421/164D NS cbest __Output__ Last First Phone Bldg Room email Alanon Bart 5590 EL O'Lewis John ---- -- --- john Le Much Bo Jo^M 3406 NS 165 ed@a.nl Abe-Jen Mar-Jo 3421 NS 164D cbest

-Theo-
(so many nodes and so little time ... )


In reply to Need a better way to count input lines by Theo

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (4)
    As of 2018-07-17 06:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (355 votes). Check out past polls.

      Notices?