Re: Can arrays be used as scalars?
Hi What your friend has done is assign the last element of the array @dir to $free. Perl array elements are accessed as $array_name[element_index] where the element index begins at zero. So to get the 1st element of @dir you could write print $dir[0], to get the third $dir[2] etc ... The index of the last element can be found in $#dir so to get the last element you can write $dir[$#dir]. To find out how many elements there are in an array you could write $num_elemants = $#dir + 1 or you can just evaluate @dir in a scalar context ie print scalar(@dir). If you try print @dir however you will get the whole array as the print operator works in list context not scalar context. If this is allreally confusing (sorry!) the check out thePerl docs (try 'perldoc perldata') or the Camel book, both ofwhich probably explain things a whole lot better than me ! HTH Paul - Original Message - From: RCTay To: [EMAIL PROTECTED] Sent: Sunday, November 24, 2002 8:06 AM Subject: Can arrays be used as scalars? my( @dir, $free);@dir =3D `dir`;$free =3D $dir[$#dir]; This code was written by a friend of mine. "@dir" was declared as aarray, but in the second line it is used as a scalar. How is thatpossible? There are no warnings given by the interpreter. Can you pleaseexplain why it can be used?
Re: Can arrays be used as scalars?
On 24/11/2002 08:06:35 RCTay wrote: my( @dir, $free); @dir =3D `dir`; $free =3D $dir[$#dir]; This code was written by a friend of mine. @dir was declared as a array, but in the second line it is used as a scalar. How is that possible? There are no warnings given by the interpreter. Can you please explain why it can be used? Neither the second nor the third line is valid Perl. Bareword found where operator expected at -e line 1, near 3D (Missing operator before D?) Backticks found where operator expected at -e line 1, near D `dir` (Do you need to predeclare D?) Or did you mean something else ? -- Csaba Ráduly, Software Engineer Sophos Anti-Virus email: [EMAIL PROTECTED]http://www.sophos.com US Support: +1 888 SOPHOS 9 UK Support: +44 1235 559933 ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Can arrays be used as scalars?
please do not send me anymore email, thanks! [EMAIL PROTECTED] wrote: On 24/11/2002 08:06:35 RCTay wrote:my( @dir, $free);@dir =3D `dir`;$free =3D $dir[$#dir];This code was written by a friend of mine. "@dir" was declared asaarray, butin the second line it is used as a scalar. How is thatpossible? Thereare nowarnings given by the interpreter. Can you pleaseexplain why it can beused?Neither the second nor the third line is valid Perl.Bareword found where operator expected at -e line 1, near "3D"(Missing operator before D?)Backticks found where operator expected at -e line 1, near "D `dir`"(Do you need to predeclare D?)Or did you mean something else ?--Csaba Ráduly, Software Engineer Sophos Anti-Virusemail: [EMAIL PROTECTED] http://www.sophos.comUS Support: +1 888 SOPHOS 9 UK Support: +44 1235 559933___Perl-Win32-Users mailing list[EMAIL PROTECTED]To unsubscribe: http://listserv.ActiveState.com/mailman/mysubsDo you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now