Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
P is for Practical
 
PerlMonks  

Re: Array Filter and Lookahead

by ww (Bishop)
on Nov 20, 2012 at 20:34 UTC ( #1004790=note: print w/ replies, xml ) Need Help??


in reply to Array Filter and Lookahead

Yet another approach (founded, in part, on moritz' point that the rule for dealing with decimals is UNspecified).

Given the OP, if the the spec is that trailing decimal digits should be truncated to integers (let us hope there's no hidden requirement for rounding :) ) then a two-part approach is also viable.

So, less elegantly but, IMO, very obvious in its tactics and using regular expressions similar to those proposed above, a TIMTOWTDI might look like this:

#!/usr/bin/perl use 5.014; #1004756 my @init_array = ("text text text 1 text", "1 text text text 2 text 3 ", "text 1 text text 2 text", "text text text 1 text 2 3.0", "1 2 3 4 5 6 7 8 9 10 11", "text text text text", ); my ($sub_select, $select, @selected); for $_(@init_array) { $_ =~ s/(.+)\.0\z/$1/; # Truncate trailing decimal numbers say "\t \$_: $_"; # debug ($select) = $_ =~ / .+? (?:(\d+)) (?:[\D]*) $ # EOL /x; push @selected, $select; } my $i = 0; for my $captured(@selected) { say "\t\t" . '#test_filtered' . "[$i]: " . '"' . $captured . '"'; $i++; } =head EXECUTION: C:\>perl 1004756.pl $_: text text text 1 text $_: 1 text text text 2 text 3 $_: text 1 text text 2 text $_: text text text 1 text 2 3 $_: 1 2 3 4 5 6 7 8 9 10 11 $_: text text text text #test_filtered[0]: "1" #test_filtered[1]: "3" #test_filtered[2]: "2" #test_filtered[3]: "3" #test_filtered[4]: "11" #test_filtered[5]: "" =cut


Comment on Re: Array Filter and Lookahead
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004790]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-04-19 03:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (476 votes), past polls