Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
First I've read "perldoc -q zero" and I don't seem to get it, or this problem is a bit trickier. What I'm trying to do is use SysV semaphores to pass 8 digit integers. This is part of a scheme I'm working on to let unrelated apps get a shared memory segment automatically, by just possesing a key. It all works well, except for 1 glitch. It seems that (on my linux anyways) each semaphore array element is limited to 4 digits. ( I nearly tore my last few hairs out trying to discover why 8 digit semaphores always came back as 0 ).

Anyways, I made a work around to split the 8 digit numbers into 2 4 digit sets, then place them into 2 semaphores. Now the clients just need to read the 2 semaphores, and combine them back to the original.

Now a problem occurs because perl will convert 0000 to 0, or 0230 to 230, or 0002 to 2.

So here is a little script which demonstrates the problem.

#!/usr/bin/perl use warnings; use strict; my @segtests = (32140578, 32140000, 23400230, 32146578, 32106578, 2130 +1000 ); foreach my $segment_id(@segtests){ my ($shval1,$shval2) = $segment_id =~ /(\d{4})(\d{4})/; print "$shval1 $shval2\n"; #this simulates what happens when the numbers #are passed through the semaphore $shval1 += 0; $shval2 += 0; my $reassemble = connectm($shval1,$shval2); print "$reassemble\n\n"; } sub connectm{ my ($val1,$val2) = @_; # this gives me errors # Left padding a number with 0 (no truncation): # my $padded = sprintf("%0${4}d", $val2); return $val1.$val2; }
OUTPUT:

3214 0578 3214578 3214 0000 32140 2340 0230 2340230 3214 6578 32146578 3210 6578 32106578 2130 1000 21301000
The problem comes with the first 3 tests. The middle example is easy enough, test for 0, and multiply by 10000 to get the result. The second test "2340 0230" is the difficult one. If it comes through as 230, how to I left pad a 0 to make it a numeric 0230. I figure everything has to be converted to strings, concated, then converted back to number by adding 0. ??

But I figure I would ask here, since I'm probably overlooking something, or there is some neat way of doing it. Thanks.


I'm not really a human, but I play one on earth. flash japh

In reply to zero padding by zentara

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: (7)
As of 2024-04-19 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found