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:
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.@entries = grep { my $comparison = compare_sub($top_entry, $_); $comparison > $user_defined_value; # inverse - we want to keep ones +that match } @entries;
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 |