parsing numbers from grep
Hi there,
is there a way to easily just get the number itself saved into an array?
i dont need the 'link' text.
if ( [at] ARGV) {
[at] link_numbers = grep(/^link([0-9]+)/, [at] ARGV);
}
Cheers,
Noah
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing numbers from grep
>>>>> "N" == Noah <noah-list [at] enabled.com> writes:
N> Hi there,
N> is there a way to easily just get the number itself saved into an
N> array? i dont need the 'link' text.
N> if ( [at] ARGV) {
N> [at] link_numbers = grep(/^link([0-9]+)/, [at] ARGV);
N> }
just change grep to map and it should work. grep only returns a subset
of its input list. map returns any list it generates given an input
list. in the above code the regex in list mode will return the grabbed
numbers and that is returned by map.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing numbers from grep
Noah wrote:
>
> Hi there,
>
> is there a way to easily just get the number itself saved into an array?
> i dont need the 'link' text.
>
> if ( [at] ARGV) {
> [at] link_numbers = grep(/^link([0-9]+)/, [at] ARGV);
> }
Use the block form:
if ( [at] ARGV) {
[at] link_numbers = grep { /^link([0-9]+)/; $_ = $1; } [at] ARGV;
}
Doesn't work with "link0".
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing numbers from grep
>>>>> "SHC" == Shawn H Corey <shawnhcorey [at] gmail.com> writes:
SHC> Noah wrote:
>>
>> Hi there,
>>
>> is there a way to easily just get the number itself saved into an array?
>> i dont need the 'link' text.
>>
>> if ( [at] ARGV) {
>> [at] link_numbers = grep(/^link([0-9]+)/, [at] ARGV);
>> }
SHC> Use the block form:
SHC> if ( [at] ARGV) {
SHC> [at] link_numbers = grep { /^link([0-9]+)/; $_ = $1; } [at] ARGV;
SHC> }
that modifies [at] ARGV so it is a bad idea. also it bypasses grep's purpose
of filtering a list. and as i posted, map is the correct solution
SHC> Doesn't work with "link0".
another reason to not use this solution.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing numbers from grep
Uri Guttman wrote:
> that modifies [at] ARGV so it is a bad idea. also it bypasses grep's purpose
> of filtering a list. and as i posted, map is the correct solution
map is not the correct solution since it does not filter out those which
do not match the pattern. Only grep can do that.
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing numbers from grep
Shawn H Corey wrote:
> Uri Guttman wrote:
>> that modifies [at] ARGV so it is a bad idea. also it bypasses grep's purpose
>> of filtering a list. and as i posted, map is the correct solution
>
> map is not the correct solution since it does not filter out those which
> do not match the pattern. Only grep can do that.
Did you actually try it? I didn't think so.
$ perl -le'
[at] ARGV = map "link$_", 0, 1, "xx", "[]", 666, "99", -5;
print "\ [at] ARGV = [at] ARGV";
my [at] link_numbers = grep { /^link([0-9]+)/; $_ = $1; } [at] ARGV;
print "\ [at] ARGV = [at] ARGV\n" . [at] link_numbers . ": [at] link_numbers";
'
[at] ARGV = link0 link1 linkxx link[] link666 link99 link-5
[at] ARGV = 0 1 666 99
3: 1 666 99
$ perl -le'
[at] ARGV = map "link$_", 0, 1, "xx", "[]", 666, "99", -5;
print "\ [at] ARGV = [at] ARGV";
my [at] link_numbers = map /^link([0-9]+)/, [at] ARGV;
print "\ [at] ARGV = [at] ARGV\n" . [at] link_numbers . ": [at] link_numbers";
'
[at] ARGV = link0 link1 linkxx link[] link666 link99 link-5
[at] ARGV = link0 link1 linkxx link[] link666 link99 link-5
4: 0 1 666 99
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing numbers from grep
>>>>> "SHC" == Shawn H Corey <shawnhcorey [at] gmail.com> writes:
SHC> Uri Guttman wrote:
>> that modifies [at] ARGV so it is a bad idea. also it bypasses grep's purpose
>> of filtering a list. and as i posted, map is the correct solution
SHC> map is not the correct solution since it does not filter out those which
SHC> do not match the pattern. Only grep can do that.
map will return what is grabbed in $1. if the pattern doesn't match,
nothing is grabbed so it will not return anything for that list element.
perl -le 'print join "-", map /x(\d+)/, qw(x123 y123 x x234)'
123-234
just what the doctor ordered! :)
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing numbers from grep
Shawn H Corey wrote:
> Uri Guttman wrote:
>> that modifies [at] ARGV so it is a bad idea. also it bypasses grep's purpose
>> of filtering a list. and as i posted, map is the correct solution
>
> map is not the correct solution since it does not filter out those which
> do not match the pattern. Only grep can do that.
my [at] selected = map { /before(pattern)after/ ? $1 : () } [at] all;
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing numbers from grep
>>>>> "R" == Ruud <rvtol+usenet [at] isolution.nl> writes:
R> Shawn H Corey wrote:
>> Uri Guttman wrote:
>>> that modifies [at] ARGV so it is a bad idea. also it bypasses grep's purpose
>>> of filtering a list. and as i posted, map is the correct solution
>>
>> map is not the correct solution since it does not filter out those which
>> do not match the pattern. Only grep can do that.
R> my [at] selected = map { /before(pattern)after/ ? $1 : () } [at] all;
no need for the ?: as list context will grab $1 or nothing. see my other
post for a working one liner that shows this.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing numbers from grep
Uri Guttman wrote:
> Ruud:
>> Shawn H Corey wrote:
>>> Uri Guttman wrote:
>>>> that modifies [at] ARGV so it is a bad idea. also it bypasses grep's purpose
>>>> of filtering a list. and as i posted, map is the correct solution
>>>
>>> map is not the correct solution since it does not filter out those which
>>> do not match the pattern. Only grep can do that.
>
>> my [at] selected = map { /before(pattern)after/ ? $1 : () } [at] all;
>
> no need for the ?: as list context will grab $1 or nothing. see my other
> post for a working one liner that shows this.
The context changed, because of the "Only grep can do that.".
Each way has it use.
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/