Need help chopping data in Perl

D

\Dandy\ Randy

Hey everyone, I could use some help with the following. I have written a
small Perl emailing program for our company. My script uses a file called
addressed.txt. This file contains names and email addresses separted by a
"|" character. A sample is below: Note the @ symbol is in brackets so this
post ignores the @ symbol and doesnt create a linkable email address.

Jogn Doe|john(@)email.com
Jane Doe|jane(@)email.com

The sending script opens this file and assigns its data to a @ array.

open (list, "<addresses.txt") or &error("Unable to open the data file for
reading");
flock list, 2;
@list=<list>;
close(list);

Later this data is sent to the sendmail module for sending to each
recipient. My boss has said that he doesnt want to send the full name, only
the first name ... so here is the question: How can I chop off the last name
from the name data? The first name and last name are separated by a simple
space. This is how I currently separate the names from the email addresses:

foreach $list (@list) {
chomp ($list);
($name,$email)=split(/\|/,$list);
}

I'm needing a set of commands that will take the variable $name ... and
remove any data that appears after the first space it encounters - including
the space itself. The result would be if I printed $name, only John or Jane
would be shown, the last name was removed. Hope I have explained this ok.
Thanx to all who read or respond. Code examples are greatly appriciated.

R
 
G

Gunnar Hjalmarsson

Dandy" Randy said:
I'm needing a set of commands that will take the variable $name ...
and remove any data that appears after the first space it
encounters - including the space itself.

$name =~ s/^(\S+).*/$1/;
 
J

Jürgen Exner

Dandy" Randy said:
Jogn Doe|john(@)email.com
Jane Doe|jane(@)email.com

The sending script opens this file and assigns its data to a @ array. [...]
How can I chop
off the last name from the name data? The first name and last name
are separated by a simple space.

You want to use split(), details please see 'perldoc -f split'

jue
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm needing a set of commands that will take the variable $name ...
and remove any data that appears after the first space it encounters -
including the space itself. The result would be if I printed $name,
only John or Jane would be shown, the last name was removed. Hope I
have explained this ok. Thanx to all who read or respond. Code
examples are greatly appriciated.

$name =~ s/ .*//;

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPyNRG2PeouIeTNHoEQKtBwCfQStyLwDWOWA7n6CKj4TNfXIY+4UAn317
HJC9MkXjVLshFWFT7iMn1Nba
=wGtE
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top