Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another thing - "emptying" scalars.

Status
Not open for further replies.

m4trix

Vendor
Jul 31, 2002
84
CA
I have written a program that searches files for strings. But it does a lot of looping. once the search is complete, it asks if you would like to search again. However, I've found that the array's and scalars still contain the information that was saved on the first search, so I get those results + my new ones. That's not what I want. I ended up fixing it by declaring them all = 0 at the beginning of the subroutine. For the arrays though, they now contain the value [0] so if my variable was called "@searches" I wrote
@tmp = pop(@searches);
and that would remove the 0 value from @searches. Works fine, but I feel awkward creating a new array called @tmp to get rid of that value. Is there any way to completely empty an array or scalar? nullify it?
 
You can use undef. That will "nullify" them.

Code:
undef @array;
undef $scalar;
--Derek

"Fear not the storm for this is where we grow strong."
 
Depending on how your code is structured, you can decalre the variables as my() variables at the beginning of the loop block or subroutine and they will be reset automatically each time through the loop or each time the subroutine is called. Scoping like this is a very useful tool.

jaa
 
great! thanks guys.. both of you have good ideas... Now, In your opinions, which of those two is the better way to do it? or does it matter? ie TMTOWTDI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top