Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
    0: # working on making it allow negative numbers via some
    1: # crazy form of look-behind and look-ahead
    2: 
    3: # NOTE: just because you CAN use a regex to do this
    4: # doesn't mean you should!
    5: 
    6: use Carp;
    7: 
    8: if (%results = solve_dio('5x + 7y + 4z = 50')) {
    9:   print map "$_ => $results{$_}\n", keys %results;
    10: }
    11: 
    12: if (solve_dio(x => 3, y => 2, z => 5, 102)) {
    13:   print "solution found!\n";
    14: }
    15: 
    16: 
    17: # solve_dio('3x + 2y + 5z = 102');
    18: # solve_dio(x => 3, y => 2, z => 5, 102);
    19: 
    20: sub solve_dio {
    21:   my ($sum,%args,$regex,@values);
    22: 
    23:   if (@_ > 1) {
    24:     $sum = pop;
    25:     %args = @_;
    26:   }
    27:   elsif (@_ == 1) {
    28:     ($sum,%args) = parseEQN(shift);
    29:   }
    30:   else {
    31:     croak << "USAGE";
    32: not enough args to solve_dio()
    33:   solve_dio(EQN)
    34:   solve_dio(VAR => COEFF, [VAR => COEFF, ] SUM)
    35: USAGE
    36:   }
    37: 
    38:   $regex = join " ", map "((?:@{[ 0 x $args{$_} ]})+)", keys %args;
    39: 
    40:   if (@values = (0 x $sum) =~ /^ $regex $/x) {
    41:     return 1 if !wantarray;
    42:     for (@values) {
    43:       my ($var) = each %args;
    44:       $args{$var} = length() / $args{$var};
    45:     }
    46:     return %args;
    47:   }
    48: }
    49: 
    50: sub parseEQN {
    51:   (my $EQN = shift) =~ tr/A-Za-z0-9 //cd;
    52:   my (@parts,$sum,%args);
    53: 
    54:   @parts = split ' ', $EQN;
    55:   $sum = pop @parts;
    56:   %args = map reverse(/(\d+)([a-z])/), @parts;
    57: 
    58:   return ($sum, %args);
    59: }

In reply to Diophantine Equation Solver by japhy

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 pondering the Monastery: (8)
As of 2024-04-18 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found