in reply to Re: Re: Re: Re: Printing last element of hash (unintentionally)
in thread Printing last element of hash (unintentionally)

A /g modifier on a replace (s///) operator replaces all matches (and thus removes all [more] substrings, so there's nothing to match for the following iterations). Leave the /g modifier out, and it will replace the first match (leaving the rest to be replaced by the next match(es)).

See the section on s/PATTERN/REPLACEMENT/egimosx in perlop:

Options are: e Evaluate the right side as an expression. g Replace globally, i.e., all occurrences. i Do case-insensitive pattern matching. m Treat string as multiple lines. o Compile pattern only once. s Treat string as single line. x Use extended regular expressions.

Joost.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Printing last element of hash (unintentionally)
by Anonymous Monk on Jan 03, 2004 at 23:28 UTC
    I removed the /g but it still doesn't do what it should. I have to entries in my hash, and it prints the last one twice rather than the first and the last once each. :(