extracting just a bit with split, discard all else

extracting just a bit with split, discard all else

am 15.11.2005 19:11:22 von Justin C

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 @fields = split /:/, @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 /:/, @array ;

Though I'm pretty certain it's a non-starter.... yup, it doesn't like
that: Use of implicit plit to @_ 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.

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

am 15.11.2005 19:58:46 von Gunnar Hjalmarsson

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 @fields = split /:/, @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 /:/, @array ;

Try:

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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

am 15.11.2005 21:21:25 von Jim Gibson

In article <3tupf9FupoiaU1@individual.net>, 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).
> >
> > I can do:
> >
> > my @fields = split /:/, @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 /:/, @array ;
>
> Try:
>
> my $want_this = ( split /:/, @array )[3];

Gunnar: that gives me nothing, i suppose because @array is evaluated in
scalar context (because split is expecting a string), which results in
the number of elements of @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 @want_this = map { (split/:/)[3] } @array;

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

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

am 15.11.2005 22:15:10 von Dave Turner

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 @fields = split /:/, @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 /:/, @array ;
>
> Though I'm pretty certain it's a non-starter.... yup, it doesn't like
> that: Use of implicit plit to @_ 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!

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

am 16.11.2005 00:29:01 von Gunnar Hjalmarsson

Jim Gibson wrote:
> Gunnar Hjalmarsson wrote:
>>
>> my $want_this = ( split /:/, @array )[3];
>
> Gunnar: that gives me nothing, i suppose because @array is evaluated in
> scalar context (because split is expecting a string), which results in
> the number of elements of @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

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

am 16.11.2005 01:30:50 von Justin C

On 2005-11-15, Jim Gibson 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 ( @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 @want_this = map { (split/:/)[3] } @array;

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


Justin.

--
Justin C, by the sea.

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

am 16.11.2005 01:33:56 von Justin C

On 2005-11-15, Dave Turner 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 @fields = split /:/, @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 /:/, @array ;
>>
>> Though I'm pretty certain it's a non-starter.... yup, it doesn't like
>> that: Use of implicit plit to @_ 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.

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

am 16.11.2005 02:10:59 von Purl Gurl

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 /:/, @array )[3];

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

Stringify your array and your code works fine.

Purl Gurl

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

am 16.11.2005 03:24:29 von Matt Garrish

"Purl Gurl" wrote in message
news:54WdnRh0kbPYGufeRVn-pA@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 /:/, @array )[3];
>
> my $want_this = ( split /:/, "@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

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

am 16.11.2005 03:34:59 von Matt Garrish

"Dave Turner" wrote in message
news:437a4fde$0$54819$742ec2ed@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 @fields = split /:/, @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 /:/, @array ;
>>
>> Though I'm pretty certain it's a non-starter.... yup, it doesn't like
>> that: Use of implicit plit to @_ 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

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

am 16.11.2005 17:39:14 von Dave Turner

Justin C wrote:
> On 2005-11-15, Dave Turner 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 @fields = split /:/, @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 /:/, @array ;
>>>
>>>Though I'm pretty certain it's a non-starter.... yup, it doesn't like
>>>that: Use of implicit plit to @_ 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.

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

am 18.11.2005 22:12:58 von Joe Smith

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.

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

-Joe