SPUG: a split question . . .

Ken McGlothlen mcglk at serv.net
Mon Feb 21 22:43:03 CST 2000

Matthew Lasar <matthew at lasarletter.com> writes:

| [...] I'm working my way through a book called *SAMs Teach Yourself Perl in
| 24 Hours*.
| Let me assure you, 24 hours came and went quite a while ago.

That's because it's not a very good book.  :)  There are better ones out there;
if you need a list, let us know.  I'm sure you'll get a dozen repeated
suggestions that would make much better choices.

| Anyway, so here's this sample database program which starts with the
| following lines of code:
| #!/usr/bin/perl -w
| open(PH, "database.txt") or die "Cannot open database.txt: $!\n";
| while(<PH>) {
| 	chomp;
| 	($number, $email)=(split(/\s+/, $_)) [1,2];
| The book doesn't adequately explain the last piece of this script, the
| "[1,2];" part. What is this for?

Okay.  Let's look at this a bit.  $_, of course, is a line out of the
filehandle PH, which is coming out of database.txt.  We've already removed the
newline at the end with chomp(), and now we have a set of fields separated with
whitespace.  The split() statement chops up the line based on the whitespace,
and returns a list.  The subscript here is used like an operator; it's a slice
of an array.  In this case, it returns the second and third elements of
the list.  Those, in turn, get assigned with the = operator to $number and
$email, respectively.

Got all that?

| Also, if I've got windows 95, what should I really put after "#!"? That stuff
| looks like a unix path to me.

It is, of course.  It's been a while since I've used ActiveState's version of
Perl (which is what I'm guessing you're using), but it didn't used to matter
what you put there under that implementation---but it's a useful line to keep
around if you move the script to a Unix system.  :)  It's also useful in that I
think ActiveState will recognize the options (in this case, -w, which provides
more warnings).  However, their documentation will give you much better answers
than my rusty memory will provide.

Best of luck, and welcome to the fold().  :)

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address

More information about the spug-list mailing list