Extracting values from array elements
I have the following array: 1 2 3 4 5 6 7 8 9 Is there a way to assign individual values of each array element (1 2 3 4 5 6 7 8 9) to seperate variables so that $var1 = 1 $var2 = 2 . . . $var9 = 9 Dean Paulson Systems Test Engineer(972) 202-8164 Xalted Networks www.xipn.net 2901 Dallas Parkway Suite 200 Plano, TX 75093-5982 Xalted NetworksTM - Proprietary and Confidential. Do not copy, reproduce or disclose to other persons without the prior written consent of XaltedNetworks. We are living in the future, I'll tell you how I know. I read it in a paper, fifteen years ago. - John Prine
Re: Extracting values from array elements
On Wed, Aug 29, 2001 at 02:22:16PM -0500, Dean W. Paulson wrote: I have the following array: 1 2 3 4 5 6 7 8 9 That looks more like a matrix. What does your array really look like? @array = ( '1 2 3', '4 5 6', '7 8 9', ); OR @array = ( [1 2 3], [4 5 6], [7 8 9], ); (i.e. a list of lists). Is there a way to assign individual values of each array element (1 2 3 4 5 6 7 8 9) to seperate variables so that $var1 = 1 $var2 = 2 . . . $var9 = 9 Yes, there is, but I wouldn't suggest it. This type of sequential variable naming is exactly what arrays are designed to accommodate. What's wrong with accessing the array directly? Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
solved: Extracting values from array elements
I have solved my ealier question. Thanks to those who responded. the problem was: I have an input file which contains n rows with 3 items in each row: 1 2 3 a b c 4 5 6 The script output needs to take the items from each row and print them as: v1=1, v2=2, v3=3 v1=a, v2=b, v3=c v1=4, v2=5, v3=6 the script looks like this: foreach (@parameters){ my $line=$_; ($v1,$v2,$v3) = split (/ /,$line); print v1=$v1\n; print v2=$v2\n; print v3=$v3\n; } Dean Paulson Systems Test Engineer(972) 202-8164 Xalted Networks www.xipn.net 2901 Dallas Parkway Suite 200 Plano, TX 75093-5982 Xalted NetworksTM - Proprietary and Confidential. Do not copy, reproduce or disclose to other persons without the prior written consent of XaltedNetworks. We are living in the future, I'll tell you how I know. I read it in a paper, fifteen years ago. - John Prine
RE: solved: Extracting values from array elements
I have solved my earlier question. Thanks to those who responded. the problem was: I have an input file which contains n rows with 3 items in each row: 1 2 3 a b c 4 5 6 The script output needs to take the items from each row and print them as: v1=1, v2=2, v3=3 v1=a, v2=b, v3=c v1=4, v2=5, v3=6 the script looks like this: foreach (@parameters){ my $line=$_; ($v1,$v2,$v3) = split (/ /,$line); print v1=$v1\n; print v2=$v2\n; print v3=$v3\n; } This would actually give the following output v1=1 v2=2 v3=3 v1=a v2=b v3=c v1=4 v2=5 v3=6 you are printing a new line character after each variable. At most, you will only need it after the third value. You will not need it at all if you did not chomp the newline off of each line of the file when you read them into the array @parameters What you would want is: foreach (@parameters){ my $line=$_; ($v1,$v2,$v3) = split (/ /,$line); print v1=$v1, v2=$v2, v3=$v3\n; #if you chomped each line. print v1=$v1, v2=$v2, v3=$v3; #if you didn't. } Dean Paulson Systems Test Engineer(972) 202-8164 Xalted Networks www.xipn.net 2901 Dallas Parkway Suite 200 Plano, TX 75093-5982 Xalted NetworksTM - Proprietary and Confidential. Do not copy, reproduce or disclose to other persons without the prior written consent of XaltedNetworks. We are living in the future, I'll tell you how I know. I read it in a paper, fifteen years ago. - John Prine -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]