manishrathi has asked for the wisdom of the Perl Monks concerning the following question:
1)
to access second elements of the array, I can use following syntax
now if I am reading in a file datacapture.cgi which has following content
if I want to loop through this array using foreach, do I need to use $data_array variabke only or can I use any variable ?
foreach (@data_array)
{ print $data_array ; } #this looks like wrong syntax
2) Do we need to declare array elemnts in double quotes only or can we declare it in single quotes also ? what if array elements are declared without any quotes ? will it be a syntax error ?
@trial_array = ("first", "second", "third");
to access second elements of the array, I can use following syntax
$sec_el = $trial_array[1];
now if I am reading in a file datacapture.cgi which has following content
"this is a data file. This ie read in array. this is looped through"
now each line becomes an element of @data_array. so there are three elements $data_array[0], $data_array[1] and $data_array[2] in @data_array.$datafile = datacapture.cgi; open(DAT, $datafile); @data_array = <DAT>;
if I want to loop through this array using foreach, do I need to use $data_array variabke only or can I use any variable ?
does it mean that $loopvariable will be assigned the elelments of @data_array one by one ? or do I need to use $data_array[] directly ?foreach $loopvariable (@data_array) { print $loopvariabe ; }
foreach (@data_array)
{ print $data_array ; } #this looks like wrong syntax
2) Do we need to declare array elemnts in double quotes only or can we declare it in single quotes also ? what if array elements are declared without any quotes ? will it be a syntax error ?
Back to
Seekers of Perl Wisdom