here i want to divide 300 such that half of it get stores in $dataIn[20] and other half get stores in $data[21]

You neglected to explain why you want to do this. It's a fairly unusual thing to want to do. I suppose most likely you're trying to deal with some kind of weird binary protocol or file format, but you don't say. Consequently, it's hard to be certain what you mean by "half of it". (That was Jaap's point.) Anonymous Monk's answer is one thing you might plausibly have meant, although I doubt it. But it's hard to tell.

Your node title implies that you believe array elements only hold one byte; that is not the case. Based on your code, though, I'm guessing maybe you have some other reason for wanting values in the array that can, in some later part of the code, be used as one-byte numbers. But that's a guess, since you don't show us the part of the code that needs the one-byte numbers.

Your code also has a couple of other problems in it, that seem unrelated to the question you asked, but may potentially be related to the problem you are having. For instance, you assign a number into an array called errorlist in one line, and in the next line you assume that the number is in a scalar called var. Also, you seem to be confused about the name of the other array; is it dataIn, or just data? If you're having this type of problem, you probably should use warnings, and possibly also use strict. These things will catch that kind of error for you automatically, and warn you about it, so that you don't have to spend a lot of time tracking it down. Very handy.

In any event, you can use modular arithmetic (the % operator) to split up a number into byte-sized chunks, if you really do need that. Here is one (untested) way to do that...

my $fullsizenumber = 1027; my @bytesizechunk = splitintobytes($fullsizenumber); sub splitintobytes { my ($num) = @_; warn "Not numeric: $num" unless $num eq ($num+0); my @result; while ($num >= 256) { push @result, $num % 256; $num -= $result[-1]; $num /= 256; } return (@result, $num); }

Note that this version returns the least significant byte first; if you want the MSB first, use reverse. The need to worry about byte order is one of a number of reasons why most programmers prefer not to fiddle with byte-oriented binary file formats and protocols. (Sometimes it cannot be avoided, of course... but if it can be avoided, it should be, because it consumes time on fiddly details that you could rather spend on the actual programming logic instead.)

HTH.HAND

"In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68

In reply to Re: How to store a bigger number in 1 byte by jonadab
in thread How to store a bigger number in 1 byte by Arbariya

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.