Rob Dixon <[EMAIL PROTECTED]> wrote:
> Jeff Westman wrote:
> >
> > If I have an array and want to take the first element off and return it,
> I
> > would do it like this:
> >
> > return (@myArray) ? shift(@myArray) : undef;
> >
> > How would I do similarly with a hash? I have something like this:
> >
> >
> > return (exists $myHash{$val1} ) ? $Hash{$val2} : undef;
> >
> > But these leaves the value in the hash. I know I can save the value
> first,
> > then DELETE it, and then return it. But I'd like to do it all in one
> step.
>
> Hi Jeff.
>
> The 'shift' built-in returns 'undef' if its operand is an empty array.
>
> Likewise, 'delete' returns either the element deleted or 'undef' if
> it didn't exist.
I didn't know 'delete' returned the value as well. Simple and perfect!
> It's also usual to omit 'return' on the last line of a subroutine,
> so you could just write:
>
> shift @myArray;
>
> and
>
> delete $myHash{$val1}
>
> to do what you want.
I know perl returns the last value (statement?) by default, but doesn't it
make it more readable (or self-documenting) to the next person who may come
along what my intent is?
-Jeff
__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]