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

New Free Tools on Dev Shed!

#1
August 16th, 2013, 06:43 AM
 Lectrician
Contributing User

Join Date: Jun 2008
Posts: 55
Time spent in forums: 5 h 4 m 2 sec
Reputation Power: 6
Remove entries in an array, and work out the new structure?

If I have an array as below:

\$array[0] = "entry 0";
\$array[1] = "entry 1";
\$array[2] = "entry 2";
\$array[3] = "entry 3";
\$array[4] = "entry 4";
\$array[5] = "entry 5";

If I remove (say) the element number 1 and 4, all elements will move places downwards, number 5 moving down twice.

Is there away to create an associative array so I could grab the value of (say) \$oldarray{'2'} and the value would be the new position of old \$array[2] (so \$array[1], the value of the associative array being '1')?

I need to do this to update a seperate array of data when an array changes.

Thanks.

#2
August 16th, 2013, 08:54 AM
 shawnhcorey
wizard

Join Date: Jul 2009
Location: The Great White North
Posts: 79
Time spent in forums: 19 h 52 m 29 sec
Reputation Power: 137
Quote:
 Originally Posted by Lectrician If I have an array as below: \$array[0] = "entry 0"; \$array[1] = "entry 1"; \$array[2] = "entry 2"; \$array[3] = "entry 3"; \$array[4] = "entry 4"; \$array[5] = "entry 5"; If I remove (say) the element number 1 and 4, all elements will move places downwards, number 5 moving down twice.

How do you remove the values? Code please.

Quote:
 Originally Posted by Lectrician Is there away to create an associative array so I could grab the value of (say) \$oldarray{'2'} and the value would be the new position of old \$array[2] (so \$array[1], the value of the associative array being '1')? I need to do this to update a seperate array of data when an array changes. Thanks.

This sounds like you're using the wrong data structure(s) for the job. What are you trying to do?
__________________
__END__

I love Perl; it's the only language where you can bless your thingy.

#3
August 16th, 2013, 01:58 PM
 Laurent_R
Contributing User

Join Date: Jun 2012
Posts: 717
Time spent in forums: 6 Days 12 h 15 m 51 sec
Reputation Power: 474
Are you sure you really need to remove array elements? It might be easier to just set the elements to an empty string or to undef them:
Perl Code:
 Original - Perl Code
```undef \$array[1];undef \$array[4];
```

Now, your array looks like this:
Code:
```0  'entry 0'
1  undef
2  'entry 2'
3  'entry 3'
4  undef
5  'entry 5'```

 Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Remove entries in an array, and work out the new structure?