Gossamer Forum
Home : General : Perl Programming :

Removing An Item from an Array

Quote Reply
Removing An Item from an Array
I am asking a seemingly simple question, and hoping for a quick response.

I have a text file of message board posts. It's made up of arrays, and looks something like this.
user1|pass1|post1|email1|\user2|pass2|post2|email2|\user3|pass3|post3|email3|\ and so on

So I first split up the arrays using the \ delimeter, and secondly break each of those down to post.

I need to know how to delete one of those items (in the big array, the ones that are seperated by \'s). I can delete the first item using shift, but I may need to remove the second or third. I am trying to do something where an admin can come in, delete one of the posts and it will leave no mark. The deleting script would have the array id
($variable[id]) and would have to be able to remove a full item (including the \) from the text file, and then rewrite the text file with the remaining items of the array. Sounds like a mouthfull, but I know it can be done.

The end text file would then contain something like this (if the second big item was taken out)
user1|pass1|post1|email1|\user3|pass3|post3|email3|\

Here's the access code, should it help
if (-e "../nar/followups/$id.txt") { #if the logfile exists
open (LOGTF, "../nar/followups/$id.txt") || &ErrorMessage;
$typistf = <LOGTF>; #variable assignment
close (LOGTF);
@typistd = split(/\\/, $typistf); #split the BIG arrays
foreach $type (@typistd) { #the foreach loop
@follow = split(/\|/, $type); #split each smaller array
if ($follow[4] eq "") { #if there is no post image
$follow[4] = "general.gif"; #assign this one
}
if ($typist[4] eq "News") { #distinguish between news and rumor

Thanks

Quote Reply
Re: Removing An Item from an Array In reply to
You can use grep to push all values of an array that matches a test into a new array...

perldoc -f grep

Happy Coding,

--Drew
http://www.FindingHim.com
Quote Reply
Re: Removing An Item from an Array In reply to
Thanks. Maybe I'm getting too complicated for myself. Right now, my main goal is to delete one member of an array. That's really all I need.

Thanks for the suggestion, though.

Quote Reply
Re: Removing An Item from an Array In reply to
perldoc splice
perldoc pop
perldoc shift

splice will let you remove an element or chunk of elements from anywhere in an array. shift will remove the first element of an array and pop will remove the last element. if you aren't dealing with elements somewhere in the middle you will always want to use shift or pop for speed of execution.

-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: Removing An Item from an Array In reply to
For Einar's sake (and mine)...

Shift and pop make sense to me, but I find splice a bit cryptic. Could you provide a better example than perldoc did?

Happy Coding,

--Drew
http://www.FindingHim.com
Quote Reply
Re: Removing An Item from an Array In reply to
Sure thing.
You use splice to remove one or more elements from an array. All you need do is specify the starting point for the splicing and the number of elements to remove.

so if we have an array
@a = qw(1 2 3 4 5 6 7 8 9 10);
and we want to remove 4, 5, 6 and 7 from the array you start by determining where the first element (4 in this case) is in the array. since arrays are 0 based - that is, they start with the first element at 0 and go up from there - '4' is element 3 in the array. and since we want to remove 4 elements, we use:
splice(@a, 3, 4);
now @a is (1, 2, 3, 8, 9, 10)

splice actually returns the spliced elements if you need them so here's another example demonstrating that.
Code:
@a = ('a', 'b', 'c', 'd', 'e', 'f');
@b = splice(@a, 2, 3);
@a now contains ('a', 'b', 'f')
@b now contains ('c', 'd', 'e')

Another way to use splice is to replace elements in an array with other elements by specifying a list (or single element if you like) to replace the spliced elements.
so if we take the first array we used
and apply the following code:
Code:
@a = qw(1 2 3 4 5 6 7 8 9 10);
@c = qw(d e f);
@b = splice(@a, 3, 4, @c);
@a now contains (1, 2, 3, d, e, f, 8, 9, 10)
@b now contains (4, 5, 6, 7)

hope this helps.

-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';