On 7/27/10 Tue Jul 27, 2010 8:21 AM, "Erik Witkop" <ewit...@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. > > @grep_results1 = grep(/[a-zA-Z0-9]{2,4}-/, @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: @map_results1 = map(/([a-zA-Z0-9]{2,4}-)/, @sho_port_small_array); -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/