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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.