Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove preceding zero 1

Status
Not open for further replies.

rudorathod

IS-IT--Management
Jun 4, 2007
11
0
0
US
I have a variable A = 0000001111111

how can i remove the preceding zeros from the variable A and assign it to variable B

so I should have variable B = 1111111

Thankyou
 
$x = '0000011111';
$n = int $x;
print $n

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Code:
use strict;
use warnings;

my $aa = "00001111";
my $bb = $aa + 0;

print $bb;

Also, don't use $a and $b. Perl uses them as special variables in sorts...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
my $bb = $aa + 0;

cheater..... [wink]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'd like a comparison of Steve and Kevin's methods. Only for the fact that someone who writes code with us has a ton of places where he does $x*1; and I never could figure out why you would multiply a number by 1 for any reason, until I saw Steve's post and thought he might be doing something like that.


 
Forget about binary for the moment
Code:
use strict;
use warnings;

my $aa = "00003.1415926";
my $kevin = int $aa;
my $steve = $aa + 0;

print "Original:\t$aa\nKevin:\t$kevin\nSteve:\t$steve";

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Ahh.. never thought about issues like that.. you would think there would be something a little more intuitive than having to do a math operation :)
 
A star for Steve [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for the star, Kevin. Now I feel bad about the cheap shot with the variable names...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I also would have just done simple addition. $A += 0;. However, just for the amusement of it, I created a little benchmark to test which would be fastest.

And the winner is ....

int

Code:
[gray]#!/usr/bin/perl[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Benchmark[/green][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$A[/blue] = [red]'[/red][purple]0000001111111[/purple][red]'[/red][red];[/red]

[maroon]timethese[/maroon][red]([/red][fuchsia]10_000_000[/fuchsia], [red]{[/red]
	[red]'[/red][purple]baseline[/purple][red]'[/red] => [url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [red]{[/red]
		[black][b]my[/b][/black] [blue]$B[/blue] = [blue]$A[/blue]
	[red]}[/red],
	[red]'[/red][purple]addition[/purple][red]'[/red] => [black][b]sub[/b][/black] [red]{[/red]
		[black][b]my[/b][/black] [blue]$B[/blue] = [blue]$A[/blue] + [fuchsia]0[/fuchsia][red];[/red]
	[red]}[/red],
	[red]'[/red][purple]int[/purple][red]'[/red] => [black][b]sub[/b][/black] [red]{[/red]
		[black][b]my[/b][/black] [blue]$B[/blue] = [url=http://perldoc.perl.org/functions/int.html][black][b]int[/b][/black][/url] [blue]$A[/blue][red];[/red]
	[red]}[/red],
	[red]'[/red][purple]regex[/purple][red]'[/red] => [black][b]sub[/b][/black] [red]{[/red]
		[red]([/red][black][b]my[/b][/black] [blue]$B[/blue] = [blue]$A[/blue][red])[/red] =~ [red]s/[/red][purple]^0+[/purple][red]/[/red][purple][/purple][red]/[/red][red];[/red]
	[red]}[/red],
	[red]'[/red][purple]regexo[/purple][red]'[/red] => [black][b]sub[/b][/black] [red]{[/red]
		[red]([/red][black][b]my[/b][/black] [blue]$B[/blue] = [blue]$A[/blue][red])[/red] =~ [red]s/[/red][purple]^0+[/purple][red]/[/red][purple][/purple][red]/[/red][red]o[/red][red];[/red]
	[red]}[/red],
[red]}[/red][red])[/red][red];[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__END__[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]Benchmark - benchmark running times of Perl code[/li]
[/ul]
[/tt]

Output:
Code:
>perl scratch.pl
Benchmark: timing 10000000 iterations of addition, baseline, int, regex, regexo...
  baseline:  9 wallclock secs ( 5.38 usr +  0.01 sys =  5.39 CPU) @ 1856320.77/s (n=10000000)
       int: 10 wallclock secs ( 6.07 usr +  0.02 sys =  6.09 CPU) @ 1642575.56/s (n=10000000)
  addition: 13 wallclock secs ( 7.10 usr +  0.00 sys =  7.10 CPU) @ 1408450.70/s (n=10000000)
     regex: 41 wallclock secs (24.95 usr +  0.01 sys = 24.96 CPU) @ 400705.24/s (n=10000000)
    regexo: 44 wallclock secs (25.18 usr +  0.00 sys = 25.18 CPU) @ 397203.69/s (n=10000000)

For some reason the optimized regex actually performed slower than the regular regex. Obviously there is somethign wrong with my syntax in some way, but it's not worth my tiem to figure out the proper way to do it. Maybe with a cached regex?

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top