4

Repost from Perlmonks for a coworker:

I wrote a perl script to separate long lists of email separated by a semi colon. What I would like to do with the code is combine the split with the trimming of white space so I don't need two arrays. Is there away to trim while loading the first array. Output is a sorted list of names. Thanks.

#!/pw/prod/svr4/bin/perl
use warnings;
use strict;

my $file_data =
  'Builder, Bob  ;Stein, Franklin MSW; Boop, Elizabeth PHD   Cc: Bear,
+ Izzy';
my @email_list;

$file_data =~ s/CC:/;/ig;
$file_data =~ s/PHD//ig;
$file_data =~ s/MSW//ig;

my @tmp_data = split( /;/, $file_data );

foreach my $entry (@tmp_data) {
    $entry =~ s/^[ \t]+|[ \t]+$//g;
    push( @email_list, $entry );
}

foreach my $name ( sort(@email_list) ) {
    print "$name \n";
}
2
  • 2
    Have you tried: split /\s*;\s*/, $file_data Aug 11, 2009 at 17:15
  • I think you could use unpack() as well but I can't try it right now and I don't grok unpack() enough to offer you a solution without it.
    – Makis
    Aug 11, 2009 at 17:36

7 Answers 7

11

You don't have to do both operations in one go using the same function. Sometimes performing the actions separately can be more clear. That is, split first, then strip the whitespace off of each element (and then sort the result):

@email_list =
    sort(
        map {
                s/\s*(\S+)\s*/\1/; $_
            }
            split ';', $file_data
    );

EDIT: Stripping more than one part of a string at the same time can lead to pitfalls, e.g. Sinan's point below about leaving trailing spaces in the "Elizabeth" portion. I coded that snippet with the assumption that the name would not have internal whitespace, which is actually quite wrong and would have stood out as incorrect if I had consciously noticed it. The code is much improved (and also more readable) below:

@email_list =
    sort(
        map {   
                s/^\s+//;  # strip leading spaces
                s/\s+$//;  # strip trailing spaces
                $_         # return the modified string
            }
            split ';', $file_data
    );
2
  • Apparently cannot have multiple spaces in comments but there are for spaces after Elizabeth. Aug 13, 2009 at 3:22
  • > First... Second... very good points! Edited response above.
    – Ether
    Aug 13, 2009 at 16:36
11

If you don't need to trim the first and final element, this will do the trick:

@email_list = split /\s*;\s*/, $file_data;

If you do need to trim the first and final element, trim $file_data first, then repeat as above. :-P

2

Well, you can do what Chris suggested, but it doesn't handle leading and trailing spaces in $file_data.

You can add handling of these like this:

$file_data =~ s/\A\s+|\s+\z//g;

Also, please note that using 2nd array was not necessary. Check this:

my $file_data = 'Builder, Bob  ;Stein, Franklin MSW; Boop, Elizabeth PHD   Cc: Bear, Izzy';
my @email_list;

$file_data =~ s/CC:/;/ig;
$file_data =~ s/PHD//ig;
$file_data =~ s/MSW//ig;

my @tmp_data = split( /;/, $file_data );

foreach my $entry (@tmp_data) {
    $entry =~ s/^[ \t]+|[ \t]+$//g;
}

foreach my $name ( sort(@tmp_data) ) {
    print "$name \n";
}
1
my @email_list = map { s/^[ \t]+|[ \t]+$//g; $_ } split /;/, $file_data;

or the more elegant:

use Algorithm::Loops "Filter";
my @email_list = Filter { s/^[ \t]+|[ \t]+$//g } split /;/, $file_data;
0
1

See How do I strip blank space from the beginning/end of a string? in the FAQ.

@email_list = sort map {
    s/^\s+//; s/\s+$//; $_
} split ';', $file_data;

Now, note also that a for loop aliases each element of an array, so

@email_list = sort split ';', $file_data;

for (@email_list) {
    s/^\s+//;
    s/\s+$//;
}

would also work.

0

My turn:

my @fields = grep { $_ } split m/\s*(?:;|^|$)\s*/, $record;

It also strips the first and last elements as well. If grep is overkill for getting rid of the first element:

my ( undef, @fields ) = split m/\s*(?:;|^|$)\s*/, $record;

works if you know that there is a space, but that's not likely, so

my @fields = split m/\s*(?:;|^|$)\s*/, $record;
shift @fields unless $fields[0];

is the most sure way to do it.

-1

Barring some minor sintax error, this should do the whole work for you. Oh, list operations, how beautiful you are!

print join (" \n", sort { $a <=> $b } map { s/^[ \t]+|[ \t]+$//g } split (/;/, $file_data));
2
  • 1
    Having map return the result of s/// is not very useful. Try map { s/...//g; $_ }
    – ysth
    Aug 11, 2009 at 17:47
  • And you probably don't mean a numeric sort.
    – ysth
    Aug 11, 2009 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.