Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This is an attempt to save a thread in SoPW which I thought can be useful for others.

There are already incredible answers in the thread, so feel free to check in SoPW.
Please don't misunderstand me. I honestly think this can be useful. I don't mean to boost my XP here, or in that thread which I posted.

Here I wanted to test if an array is of a _same order_ with another, left array is always be greater/superset or equal to the right one. Here is my current code.
#!/usr/bin/perl -w # The original code here is constructed # thanks to ysth's suggestion use strict; my @AR = qw(a b c); # First Case my @ar = qw(z a b c); # Second Case my @ar2 = qw(b a); my @ar3 = qw(a b); my @ar4 = qw(a b c); my @ar5 = qw(z a b c); print "First case\n"; # L R print test_ordered(\@ar,\@ar2),"\n"; # Answer:False(0) print test_ordered(\@ar,\@ar3),"\n"; # Answer:True(1) print test_ordered(\@ar,\@ar4),"\n"; # Answer:True(1) print test_ordered(\@ar,\@ar5),"\n"; # Answer:True(1) print "Second case\n"; print test_ordered(\@AR,\@ar2),"\n"; # Answer: False(0) print test_ordered(\@AR,\@ar3),"\n"; # Answer: True(1) print test_ordered(\@AR,\@ar4),"\n"; # Answer: True(1) sub test_ordered { #test if two arrays are ordered in same way #assuming no duplicate and left array is always #be greater/superset or equal to the right one # L R my ($ar1,$ar2)= @_; my %h; @h{@$ar1} = (0..$#{$ar1}); my $decision = grep($h{$ar2[$_]} != $_, 0..$#{$ar2}) ? 0 : 1; return $decision; }
Currently the code give correct answer for the Second Case. How can I modify it such that it's also correct for the First Case?
Now it prints:
First Case: Second Case: 0 0 0 1 0 1 1
The desired answer is:
First Case: Second Case: 0 0 1 1 1 1 1
Regards,
Edward
<code>

Originally posted as a Categorized Question.


In reply to How to Test if Two Arrays are Ordered in the Same Way by monkfan

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 goofing around in the Monastery: (4)
As of 2024-04-16 19:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found