943,778 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 3607
  • Perl RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 20th, 2009
0

add elements in middle of array

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babno is offline Offline
6 posts
since Apr 2009
Apr 20th, 2009
0

Re: add elements in middle of array

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babno is offline Offline
6 posts
since Apr 2009
Apr 20th, 2009
0

Re: add elements in middle of array

splice can be used to add elements to an array.

Perl Syntax (Toggle Plain Text)
  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).
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Apr 20th, 2009
0

Re: add elements in middle of array

Click to Expand / Collapse  Quote originally posted by babno ...
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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Apr 20th, 2009
0

Re: add elements in middle of array

Click to Expand / Collapse  Quote originally posted by KevinADC ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babno is offline Offline
6 posts
since Apr 2009
Apr 21st, 2009
0

Re: add elements in middle of array

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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Apr 21st, 2009
0

Re: add elements in middle of array

Here is an example of your data as a hash of hashes:

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

or a hash of arrays:

Perl Syntax (Toggle Plain Text)
  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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Apr 21st, 2009
0

Re: add elements in middle of array

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babno is offline Offline
6 posts
since Apr 2009
Apr 21st, 2009
0

Re: add elements in middle of array

Perl Syntax (Toggle Plain Text)
  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.
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Apr 21st, 2009
0

Re: add elements in middle of array

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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC