I need to add elements in the middle of a user created array. The closest I found was splice, but from what I read it can only replace.
example of what I want to do it take list

monkey marble oreo sandle

monkey marble smile phone oreo sandle

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?

splice can be used to add elements to an array.

@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?

pop() removes the last element of an array. Maybe an example of what you are doing will help.

pop() removes the last element of an array. Maybe an example of what you are doing will help.

[SectionOne]
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:

my %hash = ( 
  SectionOne => {Entry1 => 15, Entry2 => 'hello there'},
  SectionTwo => {Entry1 => 19, Entry2 => 'hello there'},
)

or a hash of arrays:

my %hash = ( 
  SectionOne => [15, 'hello there'],
  SectionTwo => [19, 'hello there'],
)

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?

# perl -ne 'print if !(/\[SectionOne\]/ .. /\[SectionTWO\]/); ' file

Entry1=19
Entry2=hello there

use module like Config::IniFiles is still the best.

You're not supposed to post school work on the forum unless you have demonstrated effort to solve the requirements first, which you have not done yet.

no worries, I didn't listen to you and did my own annoying loops instead, so no harm done

Thanks for being so polite about it.... not.

i'm bad at that sorry, and I needed to get the stuff in so I could turn in before 12. I knew how to do it I was just searching for an easier/better way since our teacher isn't all about what makes sense

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.