Char to String conversion Ada - type-conversion

I am trying to iterate through chars and print them using Put_Line() function but it takes String parameters, not chars. Is it possible to convert char to String like I can do it with Integer using 'Image()? My code:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Main is
begin
for I in 'A' .. 'Z' loop
Put_Line(I);
end loop;
end Main;

Your problem is not only Character conversion but you need also to inform compiler what kind of Character you will use, and yes, you can use Image attribute to get String representation of a char.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Main is
begin
for I in Character range 'A' .. 'Z' loop
Put_Line(I'Image);
end loop;
end Main;
This code will work.

Related

Need explanation for `~0` vs. `2**64` with and without `use integer`

I wrote some test program printing the values of ~0 and 2**64:
#!/usr/bin/perl
use warnings;
use strict;
#use integer;
print ~0, "\n";
print 2**64, "\n";
Without use integer the program outputs
118446744073709551615
1.184467440737096e+19
With use integer the program outputs:
-1
1.184467440737096e+19
The other odd thing is that even when using print int(2**64) the number is output in scientific format still, just as if int(...) wasn't there (Still ~0 without use integer is output in "integer format").
I can force integer output using printf("%u\n", ...), however.
(Perl being used in 5.18.2 of SLES12 SP5 on x86_64)
Questions:
So why is 2**64 a "float" with and without use integer, while ~0 never is?
And with use integer when ~0 is print as -1, it still satisfies the condition ~0 > 2**63 (when I'd expect -1 not to be greater than any positive value (like 2**63).
Update
There seems to be another odd effect seen in the Perl debugger:
2^64 is an odd integer, and 2^64-1 is -2.
DB<22> if (1) { use integer; print 2**64, "\n" }
1.84467440737096e+19
DB<23> if (1) { use integer; print 2**64 - 1, "\n" }
-2
DB<13> if (1) { use integer; printf '%x', 2**64-1, "\n" }
fffffffffffffffe
DB<14> if (1) { use integer; printf '%x', 2**64, "\n" }
ffffffffffffffff
DB<15> if (1) { no integer; printf '%x', 2**64, "\n" }
ffffffffffffffff
DB<16> if (1) { no integer; printf '%x', 2**63, "\n" }
8000000000000000
So why is 2**64 a "float" with and without use integer
Exponentiation is calculated using floating point numbers and thus produces a float. I don't know why use integer doesn't force the result to be cast to a signed integer, but it doesn't. This is consistent with its documentation, which states that the pragma only affects the operands and results of:
the arithmetic operators (+, -, *, /, %, +=, -=, *=, /=, %=, and unary minus)
the comparison operators (<, <=, >, >=, ==, !=, <=>), and
the bitwise operators (|, &, ^, <<, >>, |=, &=, ^=, <<=, >>=)
In fact, it specifically excludes **.
The power operator ** is also not affected, so that 2 ** .5 is always the square root of 2.
while ~0 never is?
The machine only has operations for performing bitwise operations on integer types, and they return integer types. There's no point in converting the number to a float (and plenty of reasons not to on a build with 64-bit ints).
And with use integer when ~0 is print as -1, it still satisfies the condition ~0 > 2**63 (when I'd expect -1 not to be greater than any positive value (like 2**63).
use integer causes many operators to cast values to IV, and < is such an operator. Casting 2**63 produces -9223372036854775808 on my machine.
$ perl -M5.010 -Minteger -e'say 0 + 2**63'
-9223372036854775808
Manual page perlop(1) explains in section "Integer Arithmetic" that use integer will cause a signed interpretation of integer results, so that will explain 118446744073709551615 vs. -1.
The other thing is that with 64-bit integers 2**64 actually is a 65-bit number than cannot be presented as integer.
So that's interpreted as floating-point number.
And maybe, most important:
~0 is not 2**64, but 2**64 - 1.
One only effect I cannot explain is why int floor(2**64 - 1) isn't output as integer number like ~0 or 0xffffffffffffffff are.

concatination in do operator argument doesn't work

concatenation in do doesn't work
Can any one explain why
this code work:
do 'begin perform 2; end;';
better say doesn't generate errors
and that:
do 'begin perform '||'2'||'; end;';
generate
ERROR: syntax error at or near "||"
LINE 1: do 'begin perform '||'2'||'; end;';
The argument to DO has to be a string literal.
From the manual:
Parameters (...) code (...) This must be specified as a string literal (...)
An expression concatenating strings isn't a literal.

What is use of exclamation mark(!) in this qbasic programming?

DECLARE SUB cube(!)
INPUT "Enter a length";l
CALL cube(l)
END
SUB cube(l)
area=6*l^2
PRINT "Area of a cube",area
END SUB
This snip describes calling a subroutine in QBasic to get the area of a cube:
DECLARE SUB Cube(L!)
INPUT "Enter a length"; L!
CALL Cube(L!)
END
SUB Cube (L!)
Area! = 6 * L! ^ 2
PRINT "Area of a cube"; Area!
END SUB
The ! declares a variable as single precision.

Perl Cryptology: Encrypting/Decrypting ASCII chracters with pack and unpack functions

I need help figuring out how these two subroutines work and what values or data structures they return. Here's a minimal representation of the code:
#!/usr/bin/perl
use strict; use warnings;
# an array of ASCII encrypted characters
my #quality = ("C~#p)eOA`/>*", "DCCec)ds~~", "*^&*"); # for instance
# input the quality
# the '#' character in front deferences the subroutine's returned array ref
my #q = #{unpack_qual_to_phred(#quality)};
print pack_phred_to_qual(\#q) . "\n";
sub unpack_qual_to_phred{
my ($qual)=#_;
my $upack_code='c' . length($qual);
my #q=unpack("$upack_code",$qual);
for(my $i=0;$i<#q;$i++){
$q[$i]-=64;
}
return(\#q);
}
sub pack_phred_to_qual{
my ($q_ref)=#_;
#q=#{$q_ref};
for(my $i=0;$i<#q;$i++){
$q[$i]+=64;
}
my $pack_code='c' . int(#q);
my $qual=pack("$pack_code",#q);
return ($qual);
}
1;
From my understanding, the unpack_qual_to_phread() subroutine apparently decrypts the ASCII character elements stored in #quality. The subroutine reads in an array containing elements of ASCII characters. Each element of the array is processed and apparently decrypted. The subroutine then returns an array ref containing elements of the decrypted array. I understand this much however I'm not really familiar with the Perl functions pack and unpack. Also I was unable to find any good examples of them online.
I think the pack_phred_to_qual subroutine converts the quality array ref back into ASCII characters and prints them.
thanks. any help or suggestions are greatly appreciated. Also if someone could provide a simple example of how Perl's pack and unpack functions work that would help too.
Calculating the length is needless. Those functions can be simplified to
sub unpack_qual_to_phred { [ map $_ - 64, unpack 'c*', $_[0] ] }
sub pack_phred_to_qual { pack 'c*', map $_ + 64, #{ $_[0] } }
In encryption terms, it's a crazy simple substitution cypher. It simply subtracts 64 from the character number of each character. It could have been written as
sub encrypt { map $_ - 64, #_ }
sub decrypt { map $_ + 64, #_ }
The pack/unpack doesn't factor in the encryption/decryption at all; it's just a way of iterating over each byte.
It is fairly simple, as packs go. Is is calling unpack("c12", "C~#p)eOA/>*)` which takes each letter in turn and finds the ascii value for that letter, and then subtracts 64 from the value (well, subtracting 64 is a post-processing step, nothing to do with pack). So letter "C" is ascii 67 and 67-64 is 3. Thus the first value out of that function is a 3. Next is "~" which is ascii 126. 126-64 is 62. Next is # which is ascii 35, and 35-64 is -29, etc.
The complete set of numbers being generated from your script is:
3,62,-29,48,-23,37,15,1,32,-17,-2,-22
The "encryption" step simply reverses this process. Adds 64 and then converts to a char.
This is not a full answer to your question, but did you read perlpacktut? Or the pack/unpack docs on perldoc? Those will probably go a long way to helping you understand.
EDIT:
Here's a simple way to think of it: say you have a 4-byte number stored in memory, 1234. If that's in a perl scalar, $num, then
pack('s*', $num)
would return
π♦
or whatever the actual internal storage value of "1234" is. So pack() treated the scalar value as a string, and turned it into the actual binary representation of the number (you see "pi-diamond" printed out, because that's the ASCII representation of that number). Conversely,
unpack('s*', "π♦")
would return the string "1234".
The unpack() part of your unpack_qual_to_phred() subroutine could be simplified to:
my #q = unpack("c12", "C~#p)e0A`/>*");
which would return a list of ASCII character pairs, each pair corresponding to a byte in the second argument.

How can I convert a string to a float with Perl?

Is there any function like int() which can convert a string to float value?
I'm currently using the following code:
$input=int(substr($line,1,index($line,",")-1));
I need to convert the string returned by substr to float.
Just use it. In Perl, a string that looks like a number IS a number.
Now, if you want to be sure that the thing is a number before using it then there's a utility method in Scalar::Util that does it:
use Scalar::Util qw/looks_like_number/;
$input=substr($line,1,index($line,",")-1);
if (looks_like_number($input)) {
$input += 1; # use it as a number!
}
Based on the sample input you left in the comments, a more robust method of extracting the number is:
$line =~ /([^\[\],]+)/; # <-- match anything not square brackets or commas
$input = $1; # <-- extract match
I don't know what your data looks like, but you do realize that the third argument to substr is a length, not a position, right? Also, the first position is 0. I suspect you aren't getting the data you think you are getting, and it mostly accidently works because you are starting at the beginning of the string and are only off by one.