in reply to Capturing the nth word in a string

Your @{[$str =~ m/\w+/g]}[3] would be better written as ($str =~ m/\w+/g)[3]. I'd more likely write it as, my $nth = (split ' ', $str)[3]; though that's not exactly the same thing with respect to punctuation, etc. Whether that is as usable to you depends on your data.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Capturing the nth word in a string
by Roger (Parson) on Oct 14, 2003 at 03:51 UTC
    Yes you are right. ($str =~ m/\w+/g)[3] is better than @{[$str =~ m/\w+/g]}[3]. I came up with my original solution because I first wanted to do @{$str =~ m/\w+/g}->[3], but that was obviously wrong. So I wrapped it inside @{[ ... ]} to make it work.

    And yes I would use split normally, but that $m = $str =~ m/\w+/g idiom and davido's meditation on TMTOWTDI inspired me to come up with something similar.

      You wanted to say:
      [$str =~ m/\w+/g]->[3]