Thank you for updating the post with your code. Here's a modified version that might behave more to your liking (but I'm not sure). Comments that I've added start with "###":
#!/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
Compared to what you posted, this version:

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

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.