Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 19th, 2013, 10:30 AM
 prlnoob
Registered User

Join Date: Aug 2013
Posts: 1
Time spent in forums: 21 m 4 sec
Reputation Power: 0
Using the value of an array element as the position marker of another array.

Hi,

I'm wondering how I can use the numeric value of one array element, as an position marker for a second array.

For example... I have

\$fieldsA[0] contains "3".

I'd like to use the "3" as a position marker for a second array, as in...

\$fieldsB[3] = "Some other value"

To be even more accurate in my array positions, I think I'd need to subtract 1 from the array element's position, as in...

\$fieldsB[3 -1] = "Some other value"

Now I know this most likely isn't the right code, but that's the reason I'm asking the experts. ;-)

Thanks.

#2
August 19th, 2013, 01:00 PM
 Laurent_R
Contributing User

Join Date: Jun 2012
Posts: 718
Time spent in forums: 6 Days 12 h 30 m 15 sec
Reputation Power: 474
Sure you can do that. Sometime, this is exactly what you need, using the values of an array as subscriupts of another array. Or the value of a hash as a key to another hash. Nothing wrong with these ideas, per se.

But this looks as a XY problem: you need to solve the X problem, you think that you could do it using Y, and ask is there is any better way than Y. But you did not tel what X is.

Your construction may be perfectly legitimate, or may be it would be much better to do something else (possibly a nested data structure such an array of arrays would be better, or maybe not). But we cannot say so long as you don't say what you are really trying to achieve.

 Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Using the value of an array element as the position marker of another array.