add elements in middle of array

Reply

Join Date: Apr 2009
Posts: 6
Reputation: babno is an unknown quantity at this point 
Solved Threads: 0
babno babno is offline Offline
Newbie Poster

add elements in middle of array

 
0
  #1
Apr 20th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: babno is an unknown quantity at this point 
Solved Threads: 0
babno babno is offline Offline
Newbie Poster

Re: add elements in middle of array

 
0
  #2
Apr 20th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: add elements in middle of array

 
0
  #3
Apr 20th, 2009
splice can be used to add elements to an array.

  1. @array = qw(1 2 4 5);
  2. splice(@array,2,0,3);
  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).
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: add elements in middle of array

 
0
  #4
Apr 20th, 2009
Originally Posted by babno View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: babno is an unknown quantity at this point 
Solved Threads: 0
babno babno is offline Offline
Newbie Poster

Re: add elements in middle of array

 
0
  #5
Apr 20th, 2009
Originally Posted by KevinADC View Post
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: add elements in middle of array

 
0
  #6
Apr 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: add elements in middle of array

 
0
  #7
Apr 21st, 2009
Here is an example of your data as a hash of hashes:

  1. my %hash = (
  2. SectionOne => {Entry1 => 15, Entry2 => 'hello there'},
  3. SectionTwo => {Entry1 => 19, Entry2 => 'hello there'},
  4. )

or a hash of arrays:

  1. my %hash = (
  2. SectionOne => [15, 'hello there'],
  3. SectionTwo => [19, 'hello there'],
  4. )
Last edited by KevinADC; Apr 21st, 2009 at 3:34 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: babno is an unknown quantity at this point 
Solved Threads: 0
babno babno is offline Offline
Newbie Poster

Re: add elements in middle of array

 
0
  #8
Apr 21st, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: add elements in middle of array

 
0
  #9
Apr 21st, 2009
  1. # perl -ne 'print if !(/\[SectionOne\]/ .. /\[SectionTWO\]/); ' file
  2.  
  3. Entry1=19
  4. Entry2=hello there

use module like Config::IniFiles is still the best.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: add elements in middle of array

 
0
  #10
Apr 21st, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC