Compared to what you posted, this version:#!/usr/bin/perl use strict; use warnings; ### open(FILEIN, "table.txt"); #numbers table which only has 0 and 1. ### let's read from __DATA__ instead (try adding your own DATA...) open FILEOUT, ">>data.txt" or die "data.txt: $!\n"; ### opens data.txt in output/append mode my @seed = split //, "01110011001111100010111101011111000100110"; my $expected = scalar @seed; while (<DATA>) { ### reads line by line from DATA which is provided b +elow chomp; ### you do want to chomp the input if ( ! /^[01]+$/ ) { warn "input line $. has characters other than 0 and 1\n"; next; } my $length = length(); if ( $length != @seed ) { warn "input line $. has $length digits instead of $expected\n" +; } my $count = 0; for my $digit ( split // ) { $seed[$count++] -= $digit; } print FILEOUT "@seed\n"; #shows you what we have read } ### close FILEIN; close FILEOUT; __DATA__ 01110011001111100010111101011111000100110 01110011001111100010111101011111000100110 1110011001111100010111101011111000100110 0011100110011111000101111010111110001001101 012345 xyz
When I ran it, everything in the output was integers (no fractional numbers with decimal points). Of course. since values are being subtracted from the seed array, (which started with 0's and 1's), the results end up as negative decimal values (e.g. if seed starts with "0" in a given position, and the first two lines of input have "1" in that position, you get "-2").
If you were hoping to get binary digit results, what do you expect "0 - 1 - 1" to produce? Maybe you want some sort of bitwise operation instead of integer subtraction? Check out perlop (look for "bitwise"), as well as the vec, pack and unpack functions. Hint: try this:
#!/usr/bin/perl -l $a="0101"; $b="1110"; print "$a & $b = " . ($a & $b); print "$a | $b = " . ($a | $b); print "$a ^ $b = " .(($a ^ $b) | "0000"); # this one is a little trick +y __OUTPUT__ 0101 & 1110 = 0100 0101 | 1110 = 1111 0101 ^ 1110 = 1011
Can you demonstrate the problem you mentioned (getting "float" numbers) using some sample data of your own? If so, please show us that data.
In reply to Re: Subtraction by digit
by graff
in thread Subtraction by digit
by zli034
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |