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)..

@map_results1 = map { /^[Ff].*?(.{3})$/; $1} grep { /^[Ff]/ } @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,

> @map_results1 = map { substr $_, -3 } grep { /^[Ff]/ } @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 <ewit...@gmail.com> wrote:

> On Jul 27, 11:34 pm, u...@stemsystems.com ("Uri Guttman") wrote:
> > >>>>> "EW" == Erik Witkop <ewit...@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> @final_results = map { m/^[a-zA-Z0-9]{3,4}?$/$1/ }
> @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...@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:
>
> @map_results1 = map(/(^[^Ff]{3})/, @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-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to