| | |
add elements in middle of array
![]() |
splice can be used to add elements to an array.
In the above code:
2 - is the place in the array you are splicing (its the index value, not the element value)
0 - is the number of elements in the array to replace (we are not replacing any, only adding to the array, so use 0)
3 - is the replacement list (just one element in this case).
Perl Syntax (Toggle Plain Text)
@array = qw(1 2 4 5); splice(@array,2,0,3); print "$_\n" for @array;
In the above code:
2 - is the place in the array you are splicing (its the index value, not the element value)
0 - is the number of elements in the array to replace (we are not replacing any, only adding to the array, so use 0)
3 - is the replacement list (just one element in this case).
•
•
•
•
just ran into 2nd problem as I was coding other parts. one of the things I must do is delete everything between occurences of the word "section". I got this fine, but what if it is the last section in the array? How would I get the program to know this and delete the correct number of elements?
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
pop() removes the last element of an array. Maybe an example of what you are doing will help.
Entry1=15
Entry2=hello there
[SectionTWO]
Entry1=19
Entry2=hello there
you enter that you want to delete SectionOne, and it will go through every line of the array until it finds a line with Section in it. it will then delete everything between the 2. This however wouldn't work for SectionTwo
My guess is you are using the wrong type of data struture for whatever it is you are doing. A hash might work better or an even more complex data set like an array of arrays or array of hashes. A hash of arrays would make deleting "sections" easy, if the sections are unique and can be used as hash keys.
Here is an example of your data as a hash of hashes:
or a hash of arrays:
Perl Syntax (Toggle Plain Text)
my %hash = ( SectionOne => {Entry1 => 15, Entry2 => 'hello there'}, SectionTwo => {Entry1 => 19, Entry2 => 'hello there'}, )
or a hash of arrays:
Perl Syntax (Toggle Plain Text)
my %hash = ( SectionOne => [15, 'hello there'], SectionTwo => [19, 'hello there'], )
Last edited by KevinADC; Apr 21st, 2009 at 3:34 am.
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
thanks for suggestion. Unfortunetly the teacher said he just wanted us to use an array. he is one of those people that doesn't care if theres a better way to do it. the assignment is to make a program that loads any ini file into an array and allows the user to do various things to it. these things are add section, delete section, delete entry, add entry, change entry (description is "Exact same as add entry function, just different name") yeah he is one of those guys. isn't the purpose of a function so you don't have to repeat code?
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
Perl Syntax (Toggle Plain Text)
# perl -ne 'print if !(/\[SectionOne\]/ .. /\[SectionTWO\]/); ' file Entry1=19 Entry2=hello there
use module like Config::IniFiles is still the best.
![]() |
Similar Threads
- adding array elements in a loop (C++)
- Array Reverse -need help pls (Java)
- project plz who can help me (C++)
- Arrays (Assembly)
- Output problems (C++)
- who can help me in c++ project (C++)
- Source Code that don't work? (Java)
- appending and subtracting a number in an array (C++)
- Is there a way to tokenize an array of strings (C++)
Other Threads in the Perl Forum
| Thread Tools | Search this Thread |





