printing grep results in Perl
So I have an array full of elements in the following format:
32F--sometext-xxxx-x
F32-sometext12-xxx
I am only interested in the first portion before the first hyphen.
I am able to grep to get that portion.
[at] grep_results1 = grep(/[a-zA-Z0-9]{2,4}-/, [at] sho_port_small_array);
Now I want to print the GREP MATCH, e.g. 32F-.
I know in awk I can print $1 which is the grep match.
How can I do this in perl.
I would rather not split each element delimited by hypens. There must
be an easy way to print the portion that GREP actually matched on.
Can anyone help. I am terrible at awk. This script is a perl script.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: printing grep results in Perl
On 7/27/10 Tue Jul 27, 2010 8:21 AM, "Erik Witkop" <ewitkop [at] gmail.com>
scribbled:
> So I have an array full of elements in the following format:
>
>
> 32F--sometext-xxxx-x
> F32-sometext12-xxx
>
> I am only interested in the first portion before the first hyphen.
>
> I am able to grep to get that portion.
>
> [at] grep_results1 = grep(/[a-zA-Z0-9]{2,4}-/, [at] sho_port_small_array);
>
> Now I want to print the GREP MATCH, e.g. 32F-.
>
> I know in awk I can print $1 which is the grep match.
>
> How can I do this in perl.
>
> I would rather not split each element delimited by hypens. There must
> be an easy way to print the portion that GREP actually matched on.
>
> Can anyone help. I am terrible at awk. This script is a perl script.
grep returns the array elements that match, and there is no way to change
that.
You can use map to return arbitrary expressions generated from the array
elements. In this case you want to put the part you want extracted in
capturing parentheses, which are returned by applying the regular expression
to each array element in turn:
[at] map_results1 = map(/([a-zA-Z0-9]{2,4}-)/, [at] sho_port_small_array);
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: printing grep results in Perl
Erik Witkop wrote:
> So I have an array full of elements in the following format:
>
>
> 32F--sometext-xxxx-x
> F32-sometext12-xxx
>
> I am only interested in the first portion before the first hyphen.
>
> I am able to grep to get that portion.
>
> [at] grep_results1 = grep(/[a-zA-Z0-9]{2,4}-/, [at] sho_port_small_array);
>
> Now I want to print the GREP MATCH, e.g. 32F-.
>
> I know in awk I can print $1 which is the grep match.
>
> How can I do this in perl.
>
> I would rather not split each element delimited by hypens. There must
> be an easy way to print the portion that GREP actually matched on.
Use map instead of grep:
my [at] grep_results1 = map /([a-zA-Z0-9]{2,4}-)/, [at] sho_port_small_array;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: printing grep results in Perl
>>>>> "EW" == Erik Witkop <ewitkop [at] gmail.com> writes:
EW> So I have an array full of elements in the following format:
EW> 32F--sometext-xxxx-x
EW> F32-sometext12-xxx
EW> I am only interested in the first portion before the first hyphen.
EW> I am able to grep to get that portion.
grep doesn't give back portions. you seem to misunderstand what grep
does. it only returns some of the elements in its input list. it is a
filter.
EW> [at] grep_results1 = grep(/[a-zA-Z0-9]{2,4}-/, [at] sho_port_small_array);
EW> Now I want to print the GREP MATCH, e.g. 32F-.
EW> I know in awk I can print $1 which is the grep match.
use grabbing as in most regexes. don't compare perl to awk as perl is a
full programming language vs awk's whatever you call it (and i used awk
a ton before i got into perl!).
EW> How can I do this in perl.
EW> I would rather not split each element delimited by hypens. There must
EW> be an easy way to print the portion that GREP actually matched on.
don't use grep then. or use map and then grab the parts you want. map
has a similar syntax to grep but instead of filtering, it builds a new
list from its expression/code block. you can grab the part you want in
the regex and map will return it in a list.
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: printing grep results in Perl
>>>>> "EW" == Erik Witkop <ewitkop [at] gmail.com> writes:
EW> Thanks Uri.
EW> It is time that I learned map. I typically lean on grep and =~ for
EW> matching patterns. But map sounds like a great solution.
grep is NOT a regex function or has anything to do with regexes. get
that false association out of your head. grep is a filter. period. its
first arg CAN be a regex but it can be any perl expression or code
block. regexes are commonly used with grep but they are not builtin to
grep. grep treats regexes like any other expression.
also =~ is not a regex op, but a binding one. it also binds a value to
tr/// which is not a regex. but if the right side of =~ is not tr///,
m// or s///, it will treat it like it is a regex.
and map is more important to your personal perl toolkit than grep. you
can write grep with map but you can't the other way around. this means
map is more powerful, flexible and useful than grep. not that i don't
use grep but i use map much more often since you transform lists more
often than you filter them. think of filtering as one of many possible
transforms.
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: printing grep results in Perl
Thanks Uri.
It is time that I learned map. I typically lean on grep and =~ for
matching patterns. But map sounds like a great solution.
Thanks again.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: printing grep results in Perl
--------------070308060900010500080903
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Hi Uri,
I have spent half the day looking at map and I still don't get it.
I don't get the EXPR versus BLOCK and how I can treat them differently.
I think I am close but my regex is not hitting.
/ [at] final_results = map { m/^[a-zA-Z0-9]{3,4}?$/$1/ } [at] just_cust_codes;
foreach ( [at] final_results){
print "$_<br>";
}/
I see a lot of people use a question mark in there, but I am unsure why.
All I want is the regex above to be in $1 for me to access.
Any help would be appreciated.
p.s. I read perldoc -f map, and checked my camel book. I am honestly at
a loss.
--------------070308060900010500080903--
Re: printing grep results in Perl
>>>>> "EW" == Erik Witkop <ewitkop [at] gmail.com> writes:
EW> I have spent half the day looking at map and I still don't get it.
it is easier than you think.
EW> I don't get the EXPR versus BLOCK and how I can treat them differently.
that is mostly a syntax difference. you can have a single expression,
then a comma then the list OR a {} block of code, NO comma and a
list. the block allows you to declare variables, have multiple
statements, etc. some always use a block and for simple expressions it
is the same as the EXPR one.
EW> I think I am close but my regex is not hitting.
EW> [at] final_results = map { m/^[a-zA-Z0-9]{3,4}?$/$1/ } [at] just_cust_codes;
you aren't GRABBING anything. this is not a map issue but a regex
one. people showed you how to do this in replies. why didn't you use
their code?
secondly m// has TWO slashes, and you have 3. you are NOT doing a
substitution but a simple grab. and you aren't even doing a grab as
there are no parens. a regex in list context (like in a map) will return
its grabs so map will return them. no need for any $1 stuff.
EW> I see a lot of people use a question mark in there, but I am unsure why.
EW> All I want is the regex above to be in $1 for me to access.
no you don't. it will be returned if you just use the code others have
posted. then ask how that code works. you are guessing and typing random
noise in hope that something will work. you need to understand how
regexes AND map work together.
EW> Any help would be appreciated.
EW> p.s. I read perldoc -f map, and checked my camel book. I am honestly at a
EW> loss.
did you read perldoc perlretut? and then perldoc perlre? the issue is
more regex than 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: printing grep results in Perl
On Jul 27, 11:34=A0pm, u... [at] StemSystems.com ("Uri Guttman") wrote:
> >>>>> "EW" =3D=3D Erik Witkop <ewit... [at] gmail.com> writes:
>
> =A0 EW> I have spent half the day looking at map and I still don't get it=
..
>
> it is easier than you think.
>
> =A0 EW> I don't get the EXPR versus BLOCK and how I can treat them differ=
ently.
>
> that is mostly a syntax difference. you can have a single expression,
> then a comma then the list OR a {} block of code, NO comma and a
> list. the block allows you to declare variables, have multiple
> statements, etc. some always use a block and for simple expressions it
> is the same as the EXPR one.
>
> =A0 EW> I think I am close but my regex is not hitting.
>
> =A0 EW> [at] final_results =3D map { m/^[a-zA-Z0-9]{3,4}?$/$1/ } [at] just_cust_c=
odes;
>
> you aren't GRABBING anything. this is not a map issue but a regex
> one. people showed you how to do this in replies. why didn't you use
> their code?
>
> secondly m// has TWO slashes, and you have 3. you are NOT doing a
> substitution but a simple grab. and you aren't even doing a grab as
> there are no parens. a regex in list context (like in a map) will return
> its grabs so map will return them. no need for any $1 stuff.
>
> =A0 EW> I see a lot of people use a question mark in there, but I am unsu=
re why.
>
> =A0 EW> All I want is the regex above to be in $1 for me to access.
>
> no you don't. it will be returned if you just use the code others have
> posted. then ask how that code works. you are guessing and typing random
> noise in hope that something will work. you need to understand how
> regexes AND map work together.
>
> =A0 EW> Any help would be appreciated.
>
> =A0 EW> p.s. I read perldoc -f map, and checked my camel book. I am hones=
tly at a
> =A0 EW> loss.
>
> did you read perldoc perlretut? and then perldoc perlre? the issue is
> more regex than map.
>
> uri
>
> --
> Uri Guttman =A0------ =A0u... [at] stemsystems.com =A0-------- =A0http://www.s=
ysarch.com--
> ----- =A0Perl Code Review , Architecture, Development, Training, Support =
------
> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.com=
---------
Thanks everyone. I missed the earlier posts with the code. That worked
beautifully.
There is one more task that I am unable to solve.
If $_ was something like FYUY or fO76, I would like to remove the
first ^[Ff].But keep the last 3 character.
Find and Replace does not work obviously as I would lose those last 3
characters that I want. I researched the map functionality and I can't
come up with the proper code.
This doc is very good at explaining map.
http://mailman.linuxchix.org/pipermail/courses/2003-November /001368.html
But I don't see how I can still keep the last 3 characters.
I tried thinking of it in different way. I tried to do something like:
[at] map_results1 =3D map(/(^[^Ff]{3})/, [at] map_results1);
Basically saying, if it starts with NOT F or f, then grab 3
characters. Well that didn't work at all. But I understand how map
works, a little better from trying new things.
Any help on how I can remove F or f and still keep the last 3
characters in $_?
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: printing grep results in Perl
--000e0ce0cd962954bd048ce33c7b
Content-Type: text/plain; charset=ISO-8859-1
If you don't mind a newbie trying to help...
Since you want to replace something, using bare //'s won't do, since that's
an alias for m//; m, as in match. You want a substitution, s///.
The regex you are looking for should look something like this (Untested
code):
> s/
> \b #Word boundary(you could replace it with ^, from the examples
> I've seen)
> [Ff] #Word starts with F or f
> .*? #Any character, zero or more times, non-greedy
> (.{3})\b #Your three characters, captured, at the end of the word(like the
> other \b, could be replaced with $ in this situation, maybe)
> /$1/x
>
Personally, though, if you only want the first and possibly the last three
characters, I'd try finding a solution with [substr][0], even if only
because it makes me feel like I'm optimizing stuff (<-newbie feeling, feel
free to burst my bubble).
Also, your current code would return all the elements that didn't start with
F or f, plus the ones that did and should get worked on. If you only want a
list with those elements that you replaced, you might as well do a mapgrep,
as in (again, untested code)..
[at] map_results1 = map { /^[Ff].*?(.{3})$/; $1} grep { /^[Ff]/ } [at] map_results1;
>
Here, you filter the original list to those that start with F or f, then
look for the last three elements, grabbing them as elements for the
map-returned list. Suppose you could drop the ^[Ff] in the map's regex, but
it might hurt readability for the moment.
On the other hand, if you don't mind,
> [at] map_results1 = map { substr $_, -3 } grep { /^[Ff]/ } [at] map_results1;
>
Both examples would blow up (as in returning an empty string) if whatever
element that got into $_ was less than four characters long, so you'd be
best to check that?
The latter might suffer from that fact that it's not written in the
universal language of regular expressions, maybe, but knowing when to use
regexes is part of our learning process, right?
...right?
[0]http://perldoc.perl.org/functions/substr.html
On Mon, Aug 2, 2010 at 9:42 PM, Erik Witkop <ewitkop [at] gmail.com> wrote:
> On Jul 27, 11:34 pm, u... [at] StemSystems.com ("Uri Guttman") wrote:
> > >>>>> "EW" == Erik Witkop <ewit... [at] gmail.com> writes:
> >
> > EW> I have spent half the day looking at map and I still don't get it.
> >
> > it is easier than you think.
> >
> > EW> I don't get the EXPR versus BLOCK and how I can treat them
> differently.
> >
> > that is mostly a syntax difference. you can have a single expression,
> > then a comma then the list OR a {} block of code, NO comma and a
> > list. the block allows you to declare variables, have multiple
> > statements, etc. some always use a block and for simple expressions it
> > is the same as the EXPR one.
> >
> > EW> I think I am close but my regex is not hitting.
> >
> > EW> [at] final_results = map { m/^[a-zA-Z0-9]{3,4}?$/$1/ }
> [at] just_cust_codes;
> >
> > you aren't GRABBING anything. this is not a map issue but a regex
> > one. people showed you how to do this in replies. why didn't you use
> > their code?
> >
> > secondly m// has TWO slashes, and you have 3. you are NOT doing a
> > substitution but a simple grab. and you aren't even doing a grab as
> > there are no parens. a regex in list context (like in a map) will return
> > its grabs so map will return them. no need for any $1 stuff.
> >
> > EW> I see a lot of people use a question mark in there, but I am unsure
> why.
> >
> > EW> All I want is the regex above to be in $1 for me to access.
> >
> > no you don't. it will be returned if you just use the code others have
> > posted. then ask how that code works. you are guessing and typing random
> > noise in hope that something will work. you need to understand how
> > regexes AND map work together.
> >
> > EW> Any help would be appreciated.
> >
> > EW> p.s. I read perldoc -f map, and checked my camel book. I am
> honestly at a
> > EW> loss.
> >
> > did you read perldoc perlretut? and then perldoc perlre? the issue is
> > more regex than map.
> >
> > uri
> >
> > --
> > Uri Guttman ------ u... [at] stemsystems.com --------
> http://www.sysarch.com--
> > ----- Perl Code Review , Architecture, Development, Training, Support
> ------
> > --------- Gourmet Hot Cocoa Mix ----
> http://bestfriendscocoa.com---------
>
>
> Thanks everyone. I missed the earlier posts with the code. That worked
> beautifully.
>
>
> There is one more task that I am unable to solve.
>
> If $_ was something like FYUY or fO76, I would like to remove the
> first ^[Ff].But keep the last 3 character.
>
> Find and Replace does not work obviously as I would lose those last 3
> characters that I want. I researched the map functionality and I can't
> come up with the proper code.
>
> This doc is very good at explaining map.
> http://mailman.linuxchix.org/pipermail/courses/2003-November /001368.html
>
> But I don't see how I can still keep the last 3 characters.
>
> I tried thinking of it in different way. I tried to do something like:
>
> [at] map_results1 = map(/(^[^Ff]{3})/, [at] map_results1);
>
> Basically saying, if it starts with NOT F or f, then grab 3
> characters. Well that didn't work at all. But I understand how map
> works, a little better from trying new things.
>
> Any help on how I can remove F or f and still keep the last 3
> characters in $_?
>
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--000e0ce0cd962954bd048ce33c7b--
Re: printing grep results in Perl
Erik Witkop wrote:
>
> There is one more task that I am unable to solve.
>
> If $_ was something like FYUY or fO76, I would like to remove the
> first ^[Ff].But keep the last 3 character.
>
> Find and Replace does not work obviously as I would lose those last 3
> characters that I want. I researched the map functionality and I can't
> come up with the proper code.
>
> This doc is very good at explaining map.
> http://mailman.linuxchix.org/pipermail/courses/2003-November /001368.html
>
> But I don't see how I can still keep the last 3 characters.
>
> I tried thinking of it in different way. I tried to do something like:
>
> [at] map_results1 = map(/(^[^Ff]{3})/, [at] map_results1);
>
> Basically saying, if it starts with NOT F or f, then grab 3
> characters. Well that didn't work at all. But I understand how map
> works, a little better from trying new things.
>
> Any help on how I can remove F or f and still keep the last 3
> characters in $_?
s/^[fF](?=...)// for [at] map_results1;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/