LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2006, 01:53 PM   #1
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Perl: find array key by value


Hello all,

I am trying to work with Perl here. I have a simple array of numbers, which I am looping through and performing a test on. If the test is true, I need to delete (as in totally drop) that number from the array. The problem is that the number does not necessarily correspond to it's key index, especially after the first loop.

Some code:
Code:
foreach $i ($set[1]..$set[-1]) {
    if ($i % $set[0] == 0) {
        delete $set[$i];
    }
}
So I have 'delete set[$i]' there, but it will not work as keys are not aligned to values. I need a way to find the index based on the value. What I really need is some built in such as $set[indexof($i)]. If it matters, the array always begins as 2..x, and I need to iterate over 3..x, hence my array slice.

You may notice this array slice won't work for me either, as I don't want to iterate over every number between $set[1] and $set[-1], I really want to iterate over every number from $set[1] to the end of the array. This is done trivially in python as in: 'set[1:]' but I cannot figure out how to do it in Perl

Another issue is that I need the key indexes to reset after each iteration, will delete do this? If I have:
Code:
$set[0] = 1
$set[1] = 2
$set[2] = 3
and I delete '2', I need:
Code:
$set[0] = 1
$set[1] = 3
I have searched many online docs for Perl but have not been able to find a way to pull this off. Surely there is one? Before you tell me to use a hash, let me say the order of the numbers is vitally important to the rest of this script, so a hash will not do...

If there is no such function, I am more than willing to entertain other ideas on how to accomplish my goal, I just really can't think of any, and I am not exactly a wizard with Perl, obviously.

Thanks
 
Old 04-06-2006, 02:18 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
I think that a better and easier way is to create a new array and copy over the elements that you don't want to delete into the new array. Then you could just assign the new array to the old one at the end if necessary.

So maybe you could try something like
Code:
@set[1..$#set] = grep {$_ % $set[0]} @set[1..$#set];
(I'm not sure if this works; I haven't tried it.)

Last edited by spooon; 04-06-2006 at 03:12 PM.
 
Old 04-06-2006, 03:01 PM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
Hey thanks!

I just negated the test, added the numbers I want to keep to a new array, then switched it at the end of the loop (which is actually part of another loop). It works a treat now...

I generally use python, and it is difficult when you are bringing a python mindset to Perl, as you excpect things to work the same way.

Again, thanks...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
exchange value and key per element in associative array rblampain Programming 3 04-02-2006 09:07 AM
Perl Array Spacing twistedpair Programming 2 02-18-2006 07:30 PM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM
Perl string replacement within an array? Seventh Programming 1 09-07-2004 02:50 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration