extracting just a bit with split, discard all else

I have an array containing entries like:
Fred Flintstone:280:orange:brontosaurus steak:11
Wilma Flintston:140:brown:pterydactyl wings:12
Barney Rubble: 210:green:mushrooms:17

I want to extract, by the simplest means possible, field 4 (split on the
colon).

I can do:

my [at] fields = split /:/, [at] array ;
my $want_this = $fields[3] ;

However, seeing as I don't want any of the rest of the data it seems
conoluted. I'd like to do something *like*:

my $want_this = $fields[3] = split /:/, [at] array ;

Though I'm pretty certain it's a non-starter.... yup, it doesn't like
that: Use of implicit plit to [at] _ is deprecated at... Though it does
return something, just not what I want.

Thanks for any pointers you can give.


Justin.

--
Justin C by the sea.
Justin C [ Di, 15 November 2005 19:11 ] [ ID #1059013 ]

Re: extracting just a bit with split, discard all else

Justin C wrote:
> I have an array containing entries like:
> Fred Flintstone:280:orange:brontosaurus steak:11
> Wilma Flintston:140:brown:pterydactyl wings:12
> Barney Rubble: 210:green:mushrooms:17
>
> I want to extract, by the simplest means possible, field 4 (split on the
> colon).
>
> I can do:
>
> my [at] fields = split /:/, [at] array ;
> my $want_this = $fields[3] ;
>
> However, seeing as I don't want any of the rest of the data it seems
> conoluted. I'd like to do something *like*:
>
> my $want_this = $fields[3] = split /:/, [at] array ;

Try:

my $want_this = ( split /:/, [at] array )[3];

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Hjalmarsson [ Di, 15 November 2005 19:58 ] [ ID #1059014 ]

Re: extracting just a bit with split, discard all else

In article <3tupf9FupoiaU1 [at] individual.net>, Gunnar Hjalmarsson
<noreply [at] gunnar.cc> wrote:

> Justin C wrote:
> > I have an array containing entries like:
> > Fred Flintstone:280:orange:brontosaurus steak:11
> > Wilma Flintston:140:brown:pterydactyl wings:12
> > Barney Rubble: 210:green:mushrooms:17
> >
> > I want to extract, by the simplest means possible, field 4 (split on the
> > colon).
> >
> > I can do:
> >
> > my [at] fields = split /:/, [at] array ;
> > my $want_this = $fields[3] ;
> >
> > However, seeing as I don't want any of the rest of the data it seems
> > conoluted. I'd like to do something *like*:
> >
> > my $want_this = $fields[3] = split /:/, [at] array ;
>
> Try:
>
> my $want_this = ( split /:/, [at] array )[3];

Gunnar: that gives me nothing, i suppose because [at] array is evaluated in
scalar context (because split is expecting a string), which results in
the number of elements of [at] array, which ends as the first and only
element of an array, and the fourth element is undefined.

OP: You have multiple records. Which field 4 are you interested in?

From the first record:

my $want_this = ( split /:/, $array[0] )[3];

From all of the records, saved in an array:

my [at] want_this = map { (split/:/)[3] } [at] array;

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jim Gibson [ Di, 15 November 2005 21:21 ] [ ID #1059015 ]

Re: extracting just a bit with split, discard all else

Justin C wrote:
> I have an array containing entries like:
> Fred Flintstone:280:orange:brontosaurus steak:11
> Wilma Flintston:140:brown:pterydactyl wings:12
> Barney Rubble: 210:green:mushrooms:17
>
> I want to extract, by the simplest means possible, field 4 (split on the
> colon).
>
> I can do:
>
> my [at] fields = split /:/, [at] array ;
> my $want_this = $fields[3] ;
>
> However, seeing as I don't want any of the rest of the data it seems
> conoluted. I'd like to do something *like*:
>
> my $want_this = $fields[3] = split /:/, [at] array ;
>
> Though I'm pretty certain it's a non-starter.... yup, it doesn't like
> that: Use of implicit plit to [at] _ is deprecated at... Though it does
> return something, just not what I want.
>
> Thanks for any pointers you can give.
>
>
> Justin.
>

Couple of ways to do this - although they may not be the most efficient:

1.

use strict;

my $data = "Fred Flintstone:280:orange:brontosaurus steak:11";

my ($field) = ( $data =~ m!.+:.+:.+:(.+):.+!);

print $field;

2. use strict;

my $data = "Fred Flintstone:280:orange:brontosaurus steak:11";

my (undef, undef, undef, $field) = split(":", $data );

print $field;


Hope that helps!
Dave Turner [ Di, 15 November 2005 22:15 ] [ ID #1059016 ]

Re: extracting just a bit with split, discard all else

Jim Gibson wrote:
> Gunnar Hjalmarsson wrote:
>>
>> my $want_this = ( split /:/, [at] array )[3];
>
> Gunnar: that gives me nothing, i suppose because [at] array is evaluated in
> scalar context (because split is expecting a string), which results in
> the number of elements of [at] array, which ends as the first and only
> element of an array, and the fourth element is undefined.

Thanks for the correction, Jim. Should have learned not to post code
without testing it first. :(

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Hjalmarsson [ Mi, 16 November 2005 00:29 ] [ ID #1059017 ]

Re: extracting just a bit with split, discard all else

On 2005-11-15, Jim Gibson <jgibson [at] mail.arc.nasa.gov> wrote:
>
> OP: You have multiple records. Which field 4 are you interested in?

All of them. Didn't re-read what I'd typed up and only now notice it's
lacking in clarity. I use a loop, while ( [at] array ) {... etc. and pop
each one to get the bit I need.
>
> From the first record:
>
> my $want_this = ( split /:/, $array[0] )[3];

I'll give it a go, thanks.


> From all of the records, saved in an array:
>
> my [at] want_this = map { (split/:/)[3] } [at] array;

Map? Now there's a command I'm gonna need perldoc for, never heard of it
before.


Justin.

--
Justin C, by the sea.
Justin C [ Mi, 16 November 2005 01:30 ] [ ID #1060676 ]

Re: extracting just a bit with split, discard all else

On 2005-11-15, Dave Turner <dave [at] wrightpopcorn.com> wrote:
> Justin C wrote:
>> I have an array containing entries like:
>> Fred Flintstone:280:orange:brontosaurus steak:11
>> Wilma Flintston:140:brown:pterydactyl wings:12
>> Barney Rubble: 210:green:mushrooms:17
>>
>> I want to extract, by the simplest means possible, field 4 (split on the
>> colon).
>>
>> I can do:
>>
>> my [at] fields = split /:/, [at] array ;
>> my $want_this = $fields[3] ;
>>
>> However, seeing as I don't want any of the rest of the data it seems
>> conoluted. I'd like to do something *like*:
>>
>> my $want_this = $fields[3] = split /:/, [at] array ;
>>
>> Though I'm pretty certain it's a non-starter.... yup, it doesn't like
>> that: Use of implicit plit to [at] _ is deprecated at... Though it does
>> return something, just not what I want.
>>
>> Thanks for any pointers you can give.
>>
>>
>> Justin.
>>
>
> Couple of ways to do this - although they may not be the most efficient:
>
> 1.
>
> use strict;
>
> my $data = "Fred Flintstone:280:orange:brontosaurus steak:11";
>
> my ($field) = ( $data =~ m!.+:.+:.+:(.+):.+!);

Yup, it's always got the ':', seems logical.


> my $data = "Fred Flintstone:280:orange:brontosaurus steak:11";
>
> my (undef, undef, undef, $field) = split(":", $data );

Well, I can't say it's elegant but my code never is!

Thanks to all who replied for the comments. I'll get back to bashing
this when I get back to work tomorrow.


Justin.

--
Justin C, by the sea.
Justin C [ Mi, 16 November 2005 01:33 ] [ ID #1060677 ]

Re: extracting just a bit with split, discard all else

Gunnar Hjalmarsson wrote:

> Justin C wrote:

> > I have an array containing entries like:
> > Fred Flintstone:280:orange:brontosaurus steak:11
> > Wilma Flintston:140:brown:pterydactyl wings:12
> > Barney Rubble: 210:green:mushrooms:17

> > I want to extract, by the simplest means possible, field 4 (split on the
> > colon).

> Try:

> my $want_this = ( split /:/, [at] array )[3];

my $want_this = ( split /:/, " [at] array" )[3];

Stringify your array and your code works fine.

Purl Gurl
Purl Gurl [ Mi, 16 November 2005 02:10 ] [ ID #1060678 ]

Re: extracting just a bit with split, discard all else

"Purl Gurl" <purlgurl [at] purlgurl.net> wrote in message
news:54WdnRh0kbPYGufeRVn-pA [at] giganews.com...
> Gunnar Hjalmarsson wrote:
>
>> Justin C wrote:
>
>> > I have an array containing entries like:
>> > Fred Flintstone:280:orange:brontosaurus steak:11
>> > Wilma Flintston:140:brown:pterydactyl wings:12
>> > Barney Rubble: 210:green:mushrooms:17
>
>> > I want to extract, by the simplest means possible, field 4 (split on
>> > the
>> > colon).
>
>> Try:
>
>> my $want_this = ( split /:/, [at] array )[3];
>
> my $want_this = ( split /:/, " [at] array" )[3];
>
> Stringify your array and your code works fine.
>

And how would that improve anything? Explain how you're now going to get the
fourth field from the second, third, fourth... entries in the array? The
correct answer was already provided. I would suggest you read it and learn.

Matt
Matt Garrish [ Mi, 16 November 2005 03:24 ] [ ID #1060679 ]

Re: extracting just a bit with split, discard all else

"Dave Turner" <dave [at] wrightpopcorn.com> wrote in message
news:437a4fde$0$54819$742ec2ed [at] news.sonic.net...
> Justin C wrote:
>> I have an array containing entries like:
>> Fred Flintstone:280:orange:brontosaurus steak:11
>> Wilma Flintston:140:brown:pterydactyl wings:12
>> Barney Rubble: 210:green:mushrooms:17
>>
>> I want to extract, by the simplest means possible, field 4 (split on the
>> colon).
>>
>> I can do:
>>
>> my [at] fields = split /:/, [at] array ;
>> my $want_this = $fields[3] ;
>>
>> However, seeing as I don't want any of the rest of the data it seems
>> conoluted. I'd like to do something *like*:
>>
>> my $want_this = $fields[3] = split /:/, [at] array ;
>>
>> Though I'm pretty certain it's a non-starter.... yup, it doesn't like
>> that: Use of implicit plit to [at] _ is deprecated at... Though it does
>> return something, just not what I want.
>>
>> Thanks for any pointers you can give.
>>
>>
>> Justin.
>>
>
> Couple of ways to do this - although they may not be the most efficient:
>
> 1.
>
> use strict;
>
> my $data = "Fred Flintstone:280:orange:brontosaurus steak:11";
>
> my ($field) = ( $data =~ m!.+:.+:.+:(.+):.+!);
>

There are a few things that can go wrong with this regular expression:
you're using greedy operators, most importantly; you don't anchor it to the
beginning of the string (good practice, since that's what you want); and
there's also no reason to keep matching after the capture, but that's more a
performance issue. To see what I mean, try the following:

my $line = 'Fred Flintstone:280:orange:brontosaurus steak:11:Wilma
Flintston:140:brown:pterydactyl wings:12';
my ($field) = ( $line =~ m#.+:.+:.+:(.+):.+#);
print $field;

A safer regex would be the following:

my ($field) = ( $line =~ m#^[^:]+:[^:]+:[^:]+:([^:]+):#);

Matt
Matt Garrish [ Mi, 16 November 2005 03:34 ] [ ID #1060680 ]

Re: extracting just a bit with split, discard all else

Justin C wrote:
> On 2005-11-15, Dave Turner <dave [at] wrightpopcorn.com> wrote:
>
>>Justin C wrote:
>>
>>>I have an array containing entries like:
>>>Fred Flintstone:280:orange:brontosaurus steak:11
>>>Wilma Flintston:140:brown:pterydactyl wings:12
>>>Barney Rubble: 210:green:mushrooms:17
>>>
>>>I want to extract, by the simplest means possible, field 4 (split on the
>>>colon).
>>>
>>>I can do:
>>>
>>> my [at] fields = split /:/, [at] array ;
>>> my $want_this = $fields[3] ;
>>>
>>>However, seeing as I don't want any of the rest of the data it seems
>>>conoluted. I'd like to do something *like*:
>>>
>>> my $want_this = $fields[3] = split /:/, [at] array ;
>>>
>>>Though I'm pretty certain it's a non-starter.... yup, it doesn't like
>>>that: Use of implicit plit to [at] _ is deprecated at... Though it does
>>>return something, just not what I want.
>>>
>>>Thanks for any pointers you can give.
>>>
>>>
>>> Justin.
>>>
>>
>>Couple of ways to do this - although they may not be the most efficient:
>>
>>1.
>>
>>use strict;
>>
>>my $data = "Fred Flintstone:280:orange:brontosaurus steak:11";
>>
>>my ($field) = ( $data =~ m!.+:.+:.+:(.+):.+!);
>
>
> Yup, it's always got the ':', seems logical.
>
>
>
>>my $data = "Fred Flintstone:280:orange:brontosaurus steak:11";
>>
>>my (undef, undef, undef, $field) = split(":", $data );
>
>
> Well, I can't say it's elegant but my code never is!
>
> Thanks to all who replied for the comments. I'll get back to bashing
> this when I get back to work tomorrow.
>
>
> Justin.
>

No the undef isn't the most elegant way to do it, but I've used that
technique when I had a string with about 8 items and I only wanted 5 but
they weren't sequential within the string - e.g. I wanted items
1,2,4,5,7 - and the undef worked nicely there.
Dave Turner [ Mi, 16 November 2005 17:39 ] [ ID #1060681 ]

Re: extracting just a bit with split, discard all else

Dave Turner wrote:

> No the undef isn't the most elegant way to do it, but I've used that
> technique when I had a string with about 8 items and I only wanted 5 but
> they weren't sequential within the string - e.g. I wanted items
> 1,2,4,5,7 - and the undef worked nicely there.

[at] wanted = (1,2,4,5,7); # Does not need to be in numeric order
[at] results = [at] fields[ [at] wanted]; # array slice

-Joe
Joe Smith [ Fr, 18 November 2005 22:12 ] [ ID #1064037 ]
Perl » alt.perl » extracting just a bit with split, discard all else

Vorheriges Thema: can't run perl script in /var/www/ directory
Nächstes Thema: selective replacements