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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

grep will work for this, but it's not a good solution to check for an existing item, especially not via regexp, because it will always iterate over the whole list, even if the elemet is fond at the first position.

So first thought would be a hash again, withe code like that:

our @array = 'a' .. 'e'; our @figure_out = 'd' .. 'g'; { my $index = 0; my %h = map {$_=>$index++} @array; while( my($k,$v) = each %h ){ my $n = shift @figure_out or next; $h{$n} = $index++ unless exists $h{$n}; } @array = sort {$h{$a}<=>$h{$b}} keys %h; }

That may work in special cases, but `perldoc -f each` tells us not to change a hash while iterating over it, so we end up doing it like that:

{ my @new = (); my $in = sub { for(@array,@new){ return 1 if $_ eq $_[0] } return 0 }; local $_; while( @array ){ $_ = shift @array; #this is of course pseufo for really figuring out # a new value my $new = shift @figure_out or next; $in->($new) or push @array,$new; } continue { push @new,$_ } @array = @new; }

An that works.

--
http://fruiture.de

In reply to Re: Re: best data structure by fruiture
in thread best data structure by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-20 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found