Dhiraj P Nilange wrote:
>
> Hi there
Hello,
> I want to extract some word after space from some string.
> Somebody from the mailing list had given me above
> command.
>
> suppose I wanto extract 8th word...
Perl arrays are indexed starting at zero (0) so the eighth word in an
array would be i
> I want to extract some word after space from some string.
> Somebody from the mailing list had given me above
> command.
> [snip]
> but it doesnt work. If I use some array in the split
> function like:-
>
> @array=split(/\s+/,$abc);
> print $array[8];
>
> this way it works. But I dont want t
Actually it was "[split /\s+/]->[8]" not "split[/\s+/]->[8]", and you only
need to do it like that if you are using it in a "print" statement (I
couldn't get it to work otherwise). Otherwise you can use the method Nikola
explained or using the method you use below (the one that works).
Rob
--
try
$abc = ( split /\s+/ )[8];
you can use the parenth with any function that returns an array, and just
pop a [] on the end.
FYI if you want the last element (not knowing what place it's in) just use
[$#_]
This only works if you are not using @_ (or $_ I think). Use caution
for [$#_].