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 ).
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
Re: zero padding
by RazorbladeBidet (Friar) on Feb 09, 2005 at 19:27 UTC
|
When you concat, you need to
$var1.sprintf( "%04d", $var2 );
or
$var1.("0" x (4-length( $var2 ))).$var2
| [reply] [d/l] [select] |
|
Dang, did you travel back in time from after Perl 6 was released?
| [reply] |
|
| [reply] |
|
???
Update: Please explain - those work just fine on my 5.8.0 - am I missing something? (obviously!)
| [reply] |
Re: zero padding
by nobull (Friar) on Feb 09, 2005 at 20:26 UTC
|
Am I mad or is everyone here being fooled by the thread topic and missing the obvious?
sub connectm {
my ($val1,$val2) = @_;
return $val1 * 10000 + $val2;
}
| [reply] [d/l] |
|
Genius? Maybe. Mad? Probably. Right? No.
use strict;
use Test::More qw( no_plan );
sub nobull {
my ($val1,$val2) = @_;
return $val1 * 10000 + $val2;
}
sub dragonchild {
join'', map sprintf("%04d", $_), @_
}
my @tests = (
[ '3214', '0578', '32140578' ],
[ '3214', '0000', '32140000' ],
[ '2340', '0230', '23400230' ],
[ '3214', '6578', '32146578' ],
[ '3210', '6578', '32106578' ],
[ '2130', '1000', '21301000' ],
[ '0004', '6789', '00046789' ],
);
foreach my $test (@tests) {
is( nobull( $test->[0], $test->[1] ), $test->[2] );
is( dragonchild( $test->[0], $test->[1] ), $test->[2] );
}
Update: Expanded the number of tests being handled.
Being right, does not endow the right to be rude; politeness costs nothing. Being unknowing, is not the same as being stupid. Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence. Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
| [reply] [d/l] |
|
| [reply] |
|
Yea, the question (and it remains so in all of these) is that the first section is at least 1000.
Otherwise it wouldn't really be 8 bytes digits to begin with (not going to argue semantics here).
I kind of assumed that. Otherwise you'll just have to pad both sides and concat or multiply left by 10000, add to right and pad left.
Update: You could just
sprintf( "%08d", $var1*10000+$var2 )
and call it a day, but that's actually slower than any of the others :(
| [reply] [d/l] |
|
We weren't "fooled" - we were "focused" :)
Good show.
| [reply] |
|
Yes of course, you are not mad, you are a genius!! That was the kind of obvious solution that was eluding me. Thanks.
I'm not really a human, but I play one on earth.
flash japh
| [reply] |
Re: zero padding
by dragonchild (Archbishop) on Feb 09, 2005 at 19:20 UTC
|
Convert your semaphores from 0-9 to a-i using tr/0-9/a-i/ and vice versa. Then, you can have 0034, store aacd, covert it back, and you're fine.
Being right, does not endow the right to be rude; politeness costs nothing. Being unknowing, is not the same as being stupid. Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence. Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
| [reply] [d/l] |
Re: zero padding
by herveus (Prior) on Feb 09, 2005 at 19:26 UTC
|
Howdy!
$padded = substr('0000'.$raw, -4, 0);
| [reply] |
Re: zero padding
by zentara (Archbishop) on Feb 09, 2005 at 19:43 UTC
|
Thanks for the ideas. This is what I came up with and seems to work. (Had to re-read perldoc -q zero a few times)
sub connectm{
my ($val1,$val2) = @_;
print "in-> $val1 $val2\n";
my $padded = '0' x ( 4 - length( $val2 ) ) . $val2;
return ($val1.$padded);
}
I'm not really a human, but I play one on earth.
flash japh
| [reply] [d/l] |
Re: zero padding
by sh1tn (Priest) on Feb 09, 2005 at 22:33 UTC
|
Not better performance solution:
use strict;
use Benchmark;
my @convert;
for(0..10000){
$convert[$_] = '0' x (4 - length$_) . $_
}
my ($val1,$val2) = (int rand(10000),int rand(10000));
timethese 2_000_000 => {
array => sub {
@convert[$val1,$val2]
},
nobull => sub {
$val1 * 10000 + $val2
}
}
#where connectm can be
#sub connectm {
# my ($val1,$val2) = @_;
# @convert[$val1,$val2]
#}
__END__
Benchmark: timing 2000000 iterations of array, nobull...
array: 2 wallclock secs ( 0.78 usr + 0.00 sys = 0.78 CPU) @ 25
+57544.76/s (n=2000000)
nobull: 1 wallclock secs ( 0.52 usr + 0.00 sys = 0.52 CPU) @ 38
+83495.15/s (n=2000000)
| [reply] [d/l] |
|
|