Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Iterating through an array using multiple loops and removing array elements

by Tanktalus (Canon)
on Apr 24, 2014 at 04:44 UTC ( [id://1083513]=note: print w/replies, xml ) Need Help??


in reply to Iterating through an array using multiple loops and removing array elements

I have to admit, I don't get the code. As in, I don't really get what you're trying to accomplish.

But some warning flags do erupt.

The first one is that when you splice an entry out, you keep checking if you get more $comparisons that are less than or equal to whatever value you're checking against. Are you intending on removing all the entries that are lower than the current one? If so, you probably intend to use grep:

@entries = grep { my $comparison = compare_sub($top_entry, $_); $comparison > $user_defined_value; # inverse - we want to keep ones +that match } @entries;
The next flag is that it doesn't look like you do anything. A compare_sub wouldn't, at least in my mind, do anything. It just compares. And your loop doesn't do anything else. I'm not sure if there's supposed to be a do_something($???) in there somewhere. But if you're just consuming everything without doing anything, a simple @entries = (); might be faster.

The other thing that comparison makes me think of is that you're sorting things somehow. In which case I'd recommend against your merge sort (you'd have to do a binary search to keep it fast), and skip straight to using sort using your compare_sub to order things. The fact you mention that the file is pre-sorted also indicates to me that this is important in some way, so I have to wonder if you're trying to keep it - but if so, there are huge pieces missing from your sample code with regards to sorting anything. Such as pushing the entries on to an output stack. Maybe that's the "print functions" that you removed? But, if so, you'd have to indicate where the print statement goes, and which element you're printing.

So, really, everything here is just a guess. More details might be required unless someone else can glean more from this.

Replies are listed 'Best First'.
Re^2: Iterating through an array using multiple loops and removing array elements
by BiochemPhD (Novice) on Apr 24, 2014 at 05:06 UTC

    My apologies. I realize there's a lot of context missing here. It was my intention to simplify the code for readability, rather than to encumber everyone with other aspects of the code that are not relevant. The script is designed to read entries from an input file that are sorted by decreasing abundance, take the top-most (most abundant) entry and group everything else in the file that is similar (but less abundant). It should then move onto the next most-abundant entry that hasn't been grouped and search the remaining ungrouped entries for similarity and group them together. This repeats until the array of entries is exhausted.

    I want to KEEP entries that don't meet the criteria and I want to REMOVE the ones that do, and then repeat until there's nothing left.

    When I call on compare_sub it returns a value, if the value is less than or equal to a user defined value, then the entry gets printed to output and spliced out of the array. (I removed the print function from the code).

    I'm still quite new at this! Thanks for the help and I hope that clarified things a bit.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1083513]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-16 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found