This is not generic question. I have asked this question because I’m confused with creating Perl arrays.
- How do I create or define the Array or hash? What are other ways to do that?
- How do I clear a array or hash? What are other ways to do that?
- How do I create a array or hash with empty element?
- What are the ways to create hash with out values?
Lexical arrays and hashes are created empty. You can create a new lexical array or hash with
my:For the most part, you should only be using lexical arrays and hashes, but you can create package arrays and hashes with
our:@arrayand%hashmay or may not have data in them in this case (if they were previously created, this will not clear their contents). The names of these variables are lexically scoped, but the data is package scoped, so if you say:It will still print
"1 2 3 4 5\n".There are many ways to clear a hash or array. The most common is to assign an empty list to it:
You can also use
undefto clear a hash or array:You could also
pop,shift, orspliceitems off of an array:You can also modify the number of elements in and an array by assigning a number to the
$#arrayform of its name. If you assign a negative value, the array is emptied: