develooper Front page | perl.perl6.users | Postings from June 2022

Re: Dual Buf and Str question

Thread Previous | Thread Next
From:
ToddAndMargo via perl6-users
Date:
June 12, 2022 01:57
Subject:
Re: Dual Buf and Str question
Message ID:
37a09855-6e81-6efb-9ff4-23b4da0338eb@zoho.com
On 6/11/22 02:28, ToddAndMargo via perl6-users wrote:
> On 6/9/22 22:54, ToddAndMargo via perl6-users wrote:
>> Hi All,
>>
>> I can easily  get away with this in Modula2, but
>> how can I do this with Raku?
>>
>> I wish to create a single variable that can be
>> manipulated in two ways:
>>
>> 1) as a fixed length string (Str)
>>
>> 2) as a fixed length buffer (Buf)
>>
>> I can think of ways to do this, but it would
>> require separate variable and conversions routines
>> back and forth.
>>
>> Any words of Raku wisdom?
>>
>> Many thanks,
>> -T
>>
> 
> 
> Hi All,
> 
> I do believe what I am asking is not possible in
> Raku.  In Modula2, it is ridiculously easy to
> do. But there is a fly in the ointment.  There
> is a possibility of chr(0)'s in the resultant
> string.  And in Modula2, as is in C, that is
> a string terminator.  So Raku is a much better
> choice, as the length of a string is kept in
> a hidden structure and not in the the string
> itself.  And I am done with Modula2 and use
> Raku for a reason.
> 
> Anyway, I have been wrapping my mind around
> how to do this in Raku.  With some chr's and
> ord's, I can accomplish what I need.  And
> some string tricks I learned in Perl 5.
> 
> When I come up working module for this, I
> will get back.  I think you all will find it
> very useful.  Ya, I know I am being
> a bit cryptic, but all will be revealed.
> Eventually.
> 
> :-)
> 
> -T

Hi All,

Thank you for all the help and tips on this!

One of the unusual decisions Raku make concerning
string was the that would only have one element and
you could not address the indexes as you would an
array. `.elems` will always be 1.  (Use `.chars`
instead.)

But, they came up with a way to do it anyway
called `substr-rw`.

This gives me the ability to do:


     BigRoot.precision = $SourceStr.chars * 2;
     $BigNum    = BigRoot.newton's-sqrt: $PrimeNumber;
     $MyCypher  = sprintf $BigNum.base(16);
     $MyCypher ~~ s/ $( Q[.] ) //;

     loop  (my $Index=0; $Index < $SourceStr.chars; $Index += 1) {
        $ScrambleStr ~= chr( ord($SourceStr.substr-rw( $Index, 1 ) ) +^ 
ord( $MyCypher.substr-rw( $Index, 1 ) ) );
        # print $ScrambleStr ~ "\n";
     }


And now you know what I am up to.   I am placing
a string with somewhat private information up on a
web file sharing service and needed to get around:

     1) data mining
     2) prying employees eyes
     3) hackers

And yes, there is a hardened password.

So in other words, despite what the services say, there is zero trust.

Running the scrambled string back through the
above gets you your original text back.

Is it just me, or does `sprintf` just blow your
mind on how useful it is.  Beats the heck out of
a beginner trying to figure out the "encode"
intricacies.




My keeper on String indexes.  (I show how
the read, not just write.  The doc page
only shows how to write or I could not
find it):

Raku: reading and writing to a string's index:

Reference:
     https://docs.raku.org/routine/substr-rw
     https://raku.land/github:thundergnat/String::Splice

Note: Raku's Strings can not be directly addressed
       by their indexes, as you can other arrays.
       Therefore .elems will always be 1.  Use
       .chars instead

       Use `substr-rw` to work around this.


method substr-rw($from, $length = *)


 > my Str $i="abcdef"
abcdef

 > say $i.substr-rw(2, 1)
c

$i.substr-rw(2, 1) ="x"
x

 > say $i
abxdef


 > loop (my $j=0; $j < $i.chars; $j += 1) {say "$i.substr-rw($j,1)";}
a
b
x
d
e
f

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About