zentara has asked for the wisdom of the Perl Monks concerning the following question:
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 ).
I'm not really a human, but I play one on earth. flash japh
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.
OUTPUT:#!/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; }
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. ??3214 0578 3214578 3214 0000 32140 2340 0230 2340230 3214 6578 32146578 3210 6578 32106578 2130 1000 21301000
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: zero padding
by RazorbladeBidet (Friar) on Feb 09, 2005 at 19:27 UTC | |
by trammell (Priest) on Feb 09, 2005 at 20:04 UTC | |
by dakkar (Hermit) on Feb 09, 2005 at 20:19 UTC | |
by RazorbladeBidet (Friar) on Feb 09, 2005 at 20:10 UTC | |
Re: zero padding
by nobull (Friar) on Feb 09, 2005 at 20:26 UTC | |
by dragonchild (Archbishop) on Feb 09, 2005 at 20:37 UTC | |
by Tanktalus (Canon) on Feb 09, 2005 at 20:45 UTC | |
by RazorbladeBidet (Friar) on Feb 09, 2005 at 20:45 UTC | |
by RazorbladeBidet (Friar) on Feb 09, 2005 at 20:31 UTC | |
by zentara (Archbishop) on Feb 09, 2005 at 20:32 UTC | |
Re: zero padding
by dragonchild (Archbishop) on Feb 09, 2005 at 19:20 UTC | |
Re: zero padding
by herveus (Prior) on Feb 09, 2005 at 19:26 UTC | |
Re: zero padding
by zentara (Archbishop) on Feb 09, 2005 at 19:43 UTC | |
Re: zero padding
by sh1tn (Priest) on Feb 09, 2005 at 22:33 UTC |
Back to
Seekers of Perl Wisdom