Home > Archive > PERL Programming > November 2005 > extracting just a bit with split, discard all else
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 |
extracting just a bit with split, discard all else
|
|
| Justin C 2005-11-15, 9:55 pm |
| 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.
| |
| Gunnar Hjalmarsson 2005-11-15, 9:55 pm |
| 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
| |
| Jim Gibson 2005-11-15, 9:55 pm |
| In article <3tupf9FupoiaU1@individual.net>, Gunnar Hjalmarsson
<noreply@gunnar.cc> wrote:
> Justin C wrote:
>
> 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
| |
| Gunnar Hjalmarsson 2005-11-15, 9:55 pm |
| Jim Gibson wrote:
> Gunnar Hjalmarsson wrote:
>
> 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
| |
| Justin C 2005-11-15, 9:55 pm |
| On 2005-11-15, Jim Gibson <jgibson@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 ( @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.
| |
| Justin C 2005-11-15, 9:56 pm |
| On 2005-11-15, Dave Turner <dave@wrightpopcorn.com> wrote:
> Justin C wrote:
>
> 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.
| |
| Purl Gurl 2005-11-15, 9:56 pm |
| Gunnar Hjalmarsson wrote:
> Justin C wrote:
[color=darkred]
[color=darkred]
> Try:
> my $want_this = ( split /:/, @array )[3];
my $want_this = ( split /:/, "@array" )[3];
Stringify your array and your code works fine.
Purl Gurl
| |
| Matt Garrish 2005-11-15, 9:56 pm |
|
"Purl Gurl" <purlgurl@purlgurl.net> wrote in message
news:54WdnRh0kbPYGufeRVn-pA@giganews.com...
> Gunnar Hjalmarsson wrote:
>
>
>
>
>
>
> 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
| |
| Matt Garrish 2005-11-15, 9:56 pm |
|
"Dave Turner" <dave@wrightpopcorn.com> wrote in message
news:437a4fde$0$54819$742ec2ed@news.sonic.net...
> Justin C wrote:
>
> 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
| |
| Dave Turner 2005-11-16, 6:57 pm |
| Justin C wrote:
> On 2005-11-15, Dave Turner <dave@wrightpopcorn.com> wrote:
>
>
>
> Yup, it's always got the ':', seems logical.
>
>
>
>
>
> 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.
| |
| Joe Smith 2005-11-18, 6:57 pm |
| 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
|
|
|
|
|