in reply to Re: Access elements of $_
in thread Access elements of $_

I'm a bit puzzled now. I tried your solution and it works fine but so does this
$id = 1; # <--- semicolon was missing open(MYFILE,"/var/tmp/mylist.dat") or die "Cant open mylist.dat" while (<MYFILE>) { # <--- wrong file handle fixed chomp; split /,/; die "Request Id $id already present in file\n" if $_[0] eq $id; } close MYFILE;
What is the difference in using an array or doing it as above ? I thought split will split a string into an array of strings anyway. Hence I'm a bit puzzled as to why I explicitly have to use an array as per your example. Is this better programming practice ?

Replies are listed 'Best First'.
Re^3: Access elements of $_
by shmem (Chancellor) on Aug 12, 2010 at 14:29 UTC
    Hence I'm a bit puzzled as to why I explicitly have to use an array as per your example. Is this better programming practice ?

    Yes, it is - and precisely because you are puzzled :-)

    As I said above, split by default splits into the array @_ whose first element is $_[0] - and it's a deprecated usage because it leads people to be puzzled, and because @_ also is the argument array inside subroutines; so using implicit split inside subs is probably a bad idea.

Re^3: Access elements of $_
by Anonymous Monk on Aug 12, 2010 at 14:24 UTC
    Oops just realised you answered that by saying it's deprecated. Apologies I should have seen this earlier