SPUG: a split question . . .
Patterson, David S (David)
davidpa at mosaix.com
Tue Feb 22 10:40:03 CST 2000
[ Comment from the Peanut Gallery ]
This is a really cool trick! I can't believe I've never seen it before
now... I had to go back to the blue camel book (Programming Perl, Wall &
Schwartz) and find it before I could believe it. It's actually documented
on pg 48, but in a rather academic way--the example never mentions the
beauty of using it in something like split. It just proves that you can
never completely "learn" a language. People are always finding new ways to
use the tools in any toolbox...
"(A)bort (R)etry (T)ake down entire network?"
> David Patterson
> Software Engineer
> Lucent Technologies *
> 6464 185th Ave NE
> Redmond, WA 98052-6736
> 425-558-8008 x 2172
888-501-4835 Pgr
> davidpa at lucent.com
>
>
> -----Original Message-----
> From: Greg Mushen [SMTP:greg.mushen at gettyimages.com]
> Sent: Monday, February 21, 2000 8:05 PM
> To: 'Matthew Lasar'; spug-list at pm.org
> Subject: RE: SPUG: a split question . . .
>
> The [1,2] piece of the script tells the split function which values to put
> into the variables you've specified. For example, if you have a text file
> with these entries:
>
> [0] [1] [2]
>
> 02/21/00 1 greg.mushen at gettyimages.com
> 02/21/00 2 spug-list at pm.org
> 02/21/00 3 foo at bar.com
>
> ($number, $email) = (split(/\s+/, $_))[1,2];
>
> would return:
> 1 greg.mushen at gettyimages.com
> 2 spug-list at pm.org
> 3 foo at bar.com
>
> Instead, if you entered [0,2], it would return:
> 02/21/00 greg.mushen at gettyimages.com
> 02/21/00 spug-list at pm.org
> 02/21/00 foo at bar.com
>
> On my Win NT machine, I can type #!c:\perl\bin\perl.exe into the beginning
> of the script and it will execute properly. If that doesn't work, invoke
> it
> explicity with perl <your-script-name> (this assumes Perl is in your
> path).
>
> Hope this helps,
> Greg Mushen
>
> -----Original Message-----
> From: Matthew Lasar [mailto:matthew at lasarletter.com]
> Sent: Monday, February 21, 2000 7:03 PM
> To: spug-list at pm.org
> Subject: SPUG: a split question . . .
>
>
> Dear Spuggers:
>
> I have a question about a line of split script.
>
> I am a beginning Perl person. I work at the University of California Press
> in the IS department and I'm teaching myself Perl because it seems like it
> will be useful for all sorts of things. 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.
>
> 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? Also, if I've got windows 95, what should
> I really put after "#!"? That stuff looks like a unix path to me.
>
> I am grateful for the existence of this list, and for any help you care to
> offer.
>
> cordially
>
> Matthew Lasar
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> 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
>
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> 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
>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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