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

That should only change the more for each individual iteration since I'm running through each key in the hash. Each key should print a different $data{$_}.
  • Comment on Re: Re: Re: Re: Printing last element of hash (unintentionally)

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Printing last element of hash (unintentionally)
by Joost (Canon) on Jan 03, 2004 at 22:26 UTC
    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.

      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. :(