Click here to get back home

I need to extract an array from a scalar regex-wise ?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
I need to extract an array from a scalar regex-wise ? advice please wireless 802.11 05-09-2008
Posted by advice please wireless 802.11 on May 9, 2008, 2:22 pm
Please log in for more thread options
This is not a homework assignment. I'd be interested in more elegante'
solutions than mine is all..

Let's say I want to extract all of the days of the week out of a
scalar such as :

$_ =
'We went to the beach on Monday but it turned out that Sunday would
have been better. The weather report Saturday said no rain until
Thursday but I asked Tuesday (a trick!) and she said it rained
Wednesday.'

I want a regex/map/grep/etc (no iterative clauses PLEASE!) that
yields:

@days = qw( Monday Sunday Saturday Thursday Tuesday Wednesday )

My best shot is:
my @days = keys %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi };

Which, oddly, doesn't seem to work. I say "oddly", because

/((mon|tues|wednes|thurs|fri|satur|sun)day)/gi

=

0 'Saturday'
1 'Satur'
2 'Thursday'
3 'Thurs'
4 'Tuesday'
5 'Tues'
6 'Wednesday'
7 'Wednes'

yet %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi }

is an empty array!? *TILT*

I'm also not crazy about the hash solution because order may be
significant.



Posted by Mirco Wahab on May 9, 2008, 2:34 pm
Please log in for more thread options
advice please wireless 802.11 on RH8 wrote:

[ALERT: double posting w/different title]

> My best shot is:
> my @days = keys %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi };
> Which, oddly, doesn't seem to work. I say "oddly", because
> /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi
> =
>
> 0 'Saturday'
> 1 'Satur'
> 2 'Thursday'
> 3 'Thurs'
> 4 'Tuesday'
> 5 'Tues'
> 6 'Wednesday'
> 7 'Wednes'

The riddles solution involves the (?: ... ) non-capturing
(grouping only) parentheses (See the other answer to your
first posting).

Regards

M.


Posted by nolo contendere on May 9, 2008, 2:40 pm
Please log in for more thread options
On May 9, 2:22=A0pm, "advice please wireless 802.11 on RH8"
> This is not a homework assignment. I'd be interested in more elegante'
> solutions than mine is all..
>
> Let's say I want to extract all of the days of the week out of a
> scalar such as :
>
> $_ =3D
> 'We went to the beach on Monday but it turned out that Sunday would
> have been better. The weather report Saturday said no rain until
> Thursday but I asked Tuesday (a trick!) and she said it rained
> Wednesday.'
>
> I want a regex/map/grep/etc (no iterative clauses PLEASE!) that
> yields:
>
> @days =3D qw( Monday Sunday Saturday Thursday Tuesday Wednesday )
>
> My best shot is:
> =A0my @days =3D keys %{ =A0/((mon|tues|wednes|thurs|fri|satur|sun)day)/gi =
};
>
> Which, oddly, doesn't seem to work. I say "oddly", because
>
> =A0 =A0/((mon|tues|wednes|thurs|fri|satur|sun)day)/gi
>
> =3D
>
> =A0 0 =A0'Saturday'
> =A0 1 =A0'Satur'
> =A0 2 =A0'Thursday'
> =A0 3 =A0'Thurs'
> =A0 4 =A0'Tuesday'
> =A0 5 =A0'Tues'
> =A0 6 =A0'Wednesday'
> =A0 7 =A0'Wednes'
>
> yet %{ =A0/((mon|tues|wednes|thurs|fri|satur|sun)day)/gi }
>
> is an empty array!? *TILT*
>
> I'm also not crazy about the hash solution because order may be
> significant.


you need non-capturing parens around the "roots" of your weekday and
weekend names.

use strict; use warnings;
use Data::Dumper;

$_ =3D
'We went to the beach on Monday but it turned out that Sunday would
have been better. The weather report Saturday said no rain until
Thursday but I asked Tuesday (a trick!) and she said it rained
Wednesday.' ;

my @days =3D /((?:mon|tues|wednes|thurs|fri|satur|sun)day)/gi;

print Dumper( \@days ), "\n";

$ ./extract_from_str.pl
$VAR1 =3D [
'Monday',
'Sunday',
'Saturday',
'Thursday',
'Tuesday',
'Wednesday'
];


Posted by Gordon Etly on May 10, 2008, 1:23 pm
Please log in for more thread options
advice please wireless 802.11 on RH8 wrote:
> This is not a homework assignment. I'd be interested in more elegante'
> solutions than mine is all..
>
> Let's say I want to extract all of the days of the week out of a
> scalar such as :
>
> $_ =
> 'We went to the beach on Monday but it turned out that Sunday would
> have been better. The weather report Saturday said no rain until
> Thursday but I asked Tuesday (a trick!) and she said it rained
> Wednesday.'
>
> I want a regex/map/grep/etc (no iterative clauses PLEASE!) that
> yields:
>
> @days = qw( Monday Sunday Saturday Thursday Tuesday Wednesday )
>
> My best shot is:
> my @days = keys %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi };
>
> Which, oddly, doesn't seem to work. I say "oddly", because
>
> /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi
>
> =
>
> 0 'Saturday'
> 1 'Satur'
> 2 'Thursday'
> 3 'Thurs'
> 4 'Tuesday'
> 5 'Tues'
> 6 'Wednesday'
> 7 'Wednes'
>
> yet %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi }
>
> is an empty array!? *TILT*
>
> I'm also not crazy about the hash solution because order may be
> significant.

I think you want something like this:

Code:
my @days = qw( Sunday Monday Tuesday Wednesday Thursday Friday
aturday );
my @wanted_days = qw( Monday Sunday Saturday Thursday Tuesday
Wednesday );

my %days = map { $_ => m/(.*)day$/; } @days;

print map { "$_ => $days\n" } @wanted_days;


Output:
Monday => Mon
Sunday => Sun
Saturday => Satur
Thursday => Thurs
Tuesday => Tues
Wednesday => Wednes


or, if you want a three letter abbreviation,

Code:
my @days = qw( Sunday Monday Tuesday Wednesday Thursday Friday
aturday );
my @wanted_days = qw( Monday Sunday Saturday Thursday Tuesday
Wednesday );

my %days = map { $_ => m/^(.)/; } @days;

print map { "$_ => $days\n" } @wanted_days;


Output:
Monday => Mon
Sunday => Sun
Saturday => Sat
Thursday => Thu
Tuesday => Tue
Wednesday => Wed


Is this what you were after?


--
G.Etly



Posted by Uri Guttman on May 10, 2008, 5:14 pm
Please log in for more thread options

GE> advice please wireless 802.11 on RH8 wrote:

>> $_ =
>> 'We went to the beach on Monday but it turned out that Sunday would
>> have been better. The weather report Saturday said no rain until
>> Thursday but I asked Tuesday (a trick!) and she said it rained
>> Wednesday.'

>> @days = qw( Monday Sunday Saturday Thursday Tuesday Wednesday )
>>
>> My best shot is:
>> my @days = keys %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi };
>>
>> Which, oddly, doesn't seem to work. I say "oddly", because
>>
>> /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi
>>
GE> I think you want something like this:

GE> Code:
GE> my @days = qw( Sunday Monday Tuesday Wednesday Thursday Friday
GE> aturday );
GE> my @wanted_days = qw( Monday Sunday Saturday Thursday Tuesday
GE> Wednesday );

huh?? he wants to GET those days from the text, not preset them.

GE> my %days = map { $_ => m/(.*)day$/; } @days;

GE> print map { "$_ => $days\n" } @wanted_days;

and where is the input text being scanned?? that is the issue here, not
how to make a hash of day names.

GE> Is this what you were after?

no it isn't. glad to see you actually make some perl comments. try
better next time.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Similar ThreadsPosted
Dear gurus how can I extract an ARRAY from a scalar regex-wise May 9, 2008, 2:18 pm
How to extract all link from web page (loaded in variable $content) to scalar @links? September 11, 2006, 4:47 am
extract every other element from an array September 1, 2005, 2:25 pm
scalar to array? January 29, 2008, 1:50 pm
extract a column from 2 dimensional array September 13, 2004, 7:31 am
Using JOIN: Converting scalar to array August 23, 2005, 7:18 pm
assigning array to a scalar variable March 14, 2008, 4:39 am
How to pass an array and scalar as arguments to a subroutine/ June 28, 2006, 12:16 pm
substr forces scalar context with array argument November 29, 2005, 7:18 am
Mail::Box::Manager message body scalar or array? October 15, 2006, 8:33 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap