For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > Last word









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Last word
Todd Lewis

2005-08-23, 7:55 am

I'm trying to retrieve the last word from an HTML table cell stored in
an array value.
All of the words are space delimited.
I've tried using split /\s/, $$row[1] but this doesn't always return the
last
word for me since there could be 2,3, or 4 words.

Could someone point me in the right direction. I was hoping for a simple
solution not having to count the words in the cell and then retrieve the
last word based on the count of words.
Peter Scott

2005-08-23, 6:56 pm

On Tue, 23 Aug 2005 07:25:05 -0700, Todd Lewis wrote:

> I'm trying to retrieve the last word from an HTML table cell stored in
> an array value.
> All of the words are space delimited.
> I've tried using split /\s/, $$row[1] but this doesn't always return the
> last
> word for me since there could be 2,3, or 4 words.


List slice:

$last_word = (split /\s/, $row->[1]//)[-1];

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

Peter Scott

2005-08-23, 6:56 pm

On Tue, 23 Aug 2005 06:17:11 -0700, I wrote:
> $last_word = (split /\s/, $row->[1]//)[-1];


I seem beset by typos today. Don't know where that // came from. Should
be:

$last_word = (split /\s/, $row->[1])[-1];

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

rich@newsguy.com

2005-08-23, 6:56 pm

In article <430B31C1.7050005@sbcglobal.net>, Todd Lewis says...
>
>I'm trying to retrieve the last word from an HTML table cell stored in
>an array value.
>All of the words are space delimited.
>I've tried using split /\s/, $$row[1] but this doesn't always return the
>last
>word for me since there could be 2,3, or 4 words.
>
>Could someone point me in the right direction. I was hoping for a simple
>solution not having to count the words in the cell and then retrieve the
>last word based on the count of words.



I'd try something like..

@words = split /\s/, $row[1]; #<- if that's the array value you are using
$lastword = $words[$#words]; #<- $#words is the last in the array

Rich
--
Newsguy - http://newsguy.com

Todd Lewis

2005-08-23, 9:55 pm

Thanks, for all the replys. Didn't know that you could simply reference
the last element of an array by using [-1]. This seemed like it could
be a one line task maybe two, just don't have command of the language.


Peter Scott wrote:

>On Tue, 23 Aug 2005 06:17:11 -0700, I wrote:
>
>
>
>I seem beset by typos today. Don't know where that // came from. Should
>be:
>
> $last_word = (split /\s/, $row->[1])[-1];
>
>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com