Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: How do I combine SPLIT with trimming white space

by Marshall (Canon)
on Aug 11, 2009 at 20:26 UTC ( [id://787758]=note: print w/replies, xml ) Need Help??


in reply to How do I combine SPLIT with trimming white space

I tried to post before and for some reason it didn't work.
I dunno why not.

Anyway this is a different approach. Have fun!

#!usr/bin/perl -w use strict; # I suspect that you left off a ";" after Boop, Elizabeth PHD # I add added that to the test data below. my $file_data =qq( Builder, Bob ;Stein, Franklin MSW; Boop, Elizabeth PHD ; Cc: Bear, +Izzy; SomeGuy, Guy; Einstein , Albert PHD; ); # #yes, you can open a scalar for read with a filehandle! #there is some "weirdness" about this, but the idea does work! open (my $in, "<", \$file_data) or die "can't open input file $!"; while (my $line =<$in>) { next if $line =~ m/^\s*$/; # skip blank lines chomp ($line); $line =~ s/CC://i; $line =~ s/\s+$//; #gets rid of trailing \n #I suspect that this is an artifact #of the way the file is opened?? my @whole_name_titles = split(/;/,$line); foreach my $name (@whole_name_titles) { $name =~ s/\s+[A-Z]+\s*$//; #Skip trailing cap letters $name =~ s/\s+//g; #compress spaces print "$name\n"; #push to DB or whatever here... } } __END__ Prints: Builder,Bob Stein,Franklin Boop,Elizabeth Bear,Izzy SomeGuy,Guy Einstein,Albert

Replies are listed 'Best First'.
Re^2: How do I combine SPLIT with trimming white space
by Grey Fox (Chaplain) on Aug 12, 2009 at 13:59 UTC
    Hi Marshall
    One of the reasons there was no semi colon after Betty, was that she was at the end of the original e-mail To: list. One of the things I wanted to do is just pull the To: and CC: as one cut and paste. That leaves a new line between the last name and the first CC: the original code changes the CC: to a semi colon.
    Also part of the original requirements is a sorted list. Thanks though, some nice ideas.
    -- Grey Fox
    "We are grey. We stand between the darkness and the light" B5
      Grey Fox,
      Glad you got some good ideas from this thread!

      I thought something was weird with the missing semicolon - just a small detail.

      As far as sorting goes print "$name\n"; #push to DB or whatever here... is where you can just push to a list like @full_names. Here is one way of many to do the sort. Note that the "Boop" family winds up in the correct sort order.

      #!/usr/bin/perl -w use strict; my @names = ( "Builder,Bob", "Stein,Franklin", "Boop,Elizabeth", "Boop,Albert", "Bear,Izzy", "SomeGuy,Guy", "Einstein,Albert",); @names = sort by_last_name @names; print join("\n",@names),"\n"; sub by_last_name { my ($a_last, $a_first) = split (/,/,$a); my ($b_last, $b_first) = split (/,/,$b); $a_last cmp $b_last or $a_first cmp $b_first } __END__ Prints: Bear,Izzy Boop,Albert Boop,Elizabeth Builder,Bob Einstein,Albert SomeGuy,Guy Stein,Franklin
      I added the simple code to sort by first name.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2025-03-24 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (63 votes). Check out past polls.