Standard or convention for DateTime to [A-Z] string conversion? - date

I like to use DateTime.UtcNow.ToString("yyyyMMddHHmmss") as a unique-enough salt for padding instead of a random string or GUID in tests for easier debugging and sorting. However I can't use it where validation only allows alpha character strings, e.g. a name (where 'Falsehoods Programmers Believe About Names' is ignored).
Is there a standard or a convention to encode a timestamp as a [A-Z]+ string? Preferably something more efficient than roman numerals but still human readable, i.e. not a base64-like lookup-table-based variant but a logic-based one.

Not that I know of, but if you treat A as 0 and J as 9 you can simply replace all numbers from the yyyyMMddHHmmss string.

I ended up with a lookup using a mix of leet speak and calculator spelling forgoing sorting in lieu of readability.
http://www.wikihow.com/Sample/1337-Cheat-Sheet
http://www.wikihow.com/Sample/Calculator-Letter-List
http://www.inversoft.com/blog/2013/08/28/profanity-filtering-101-character-replacements-leet-speak/
var map = new Dictionary<char, char>
{
{ '0', 'O' },
{ '1', 'I' },
{ '2', 'Z' },
{ '3', 'E' },
{ '4', 'A' },
{ '5', 'S' },
{ '6', 'G' },
{ '7', 'T' },
{ '8', 'B' },
{ '9', 'P' }
};
return string.Concat(value.Select(c => map[c]));

Related

how to convert numbers from one language to english or forcing the user to write in english only?

I would like to convert Arabic numbers into english or force the user to enter english numbers only, so how to do so?
i.e 123 = ١٢٣ and so on.
For converting numbers you can simply write a function like this:
Map persianNumberMap = {
'۰': '0',
'۱': '1',
'۲': '2',
'۳': '3',
'۴': '4',
'۵': '5',
'۶': '6',
'۷': '7',
'۸': '8',
'۹': '9'
};
String convertPersianNumberToEnglish(String number) {
String converted = number;
persianNumberMap.forEach((key, value) => converted.replaceAll(key, value));
return converted;
}
But you can also use FilteringTextInputFormatter to restrict inputs of a TextField:
TextField(inputFormatters: [
FilteringTextInputFormatter.allow(RegExp("[0-9]")),
])
This TextField will only accept English number characters.

Perl vCard.pm how do I compute inputs?

The documentation for vCard shows it wants:
addresses()
Accepts/returns an arrayref that looks like:
[
{ type => ['work'], street => 'Main St', preferred => 0 },
{ type => ['home'],
pobox => 1234,
extended => 'asdf',
street => 'Army St',
city => 'Desert Base',
region => '',
post_code => '',
country => 'USA',
preferred => 1,
},
]
I must generate this call from a CSV file. I can extract the data, but cannot figure out how to pass it to $vcard->addresses.
This fails:
if (keys %addrwork > 0 )
{
$addrwork{'type'} = ['work'];
push #arraytmp, %addrwork;
}
# did we get any part of a home or work address?
if ( $#arraytmp >= 0 ) {
$vcard->addresses( #arraytmp );
Fails with:
Can't use string ("type") as an ARRAY ref while "strict refs" in use at /usr/local/share/perl/5.22.1/vCard.pm line 254, <FILE> line 2.
Accepts/returns an arrayref that looks like:
You're passing an array, not an arrayref. Fix it:
$vcard->addresses( \#arraytmp );

DBIx::Class: How can I sort on multiple substrings of a column?

I have a SQLite-database with table with a document number following this schema:
16-145-45
16-127-30
16-141-42
16-122-14
15-090-04
15-089-15
15-089-05
I'd like to sort the ResultSet on the first and last part of the number, like this. First, all documents starting with the highest two-digit prefix (16) sorted by the last 2 digits and then the same with the next block, and so on.
16-145-45
16-141-42
16-127-30
16-122-14
15-089-15
15-089-05
15-090-04
Is there a way to do this in DBIx::Class with some sort of custom order_by clause, or what would be the approach?
I have tried the following, which does not work, because the middle part of the number is also considered for sorting:
my #rs = $self->search(undef,
{
order_by => { -desc => 'me.number' }
}
);
If you want the database to sort the results, you have to use literal SQL.
Here's an example for Postgres (I added a space after the backslash to fix the syntax highlighting):
my #rs = $self->search(undef,
{
order_by => \ "split_part(number, '-', 1) || split_part(number, '-', 3) DESC",
}
);
Or, by creating an output column with the +select result set attribute:
my #rs = $self->search(undef,
{
'+select' => [
{ sort_key => \ "split_part(number, '-', 1) || split_part(number, '-', 3)" },
],
'+as' => [ qw(sort_key) ], # Make sort key accessible from DBIC.
order_by => { -desc => 'sort_key' },
}
);
Another approach is to retreive the whole unsorted result set, and sort it on the client side. DBIC doesn't have any specific features to help you with that, so simply use Perl's sort function.
Since the answer from #nwellnhof works like a charm, I just wanted to provide the corresponding syntax for SQLite, which does not know the split_part() function.
# SQL for filtering the doc number in SQLite
my #rs = $self->search(undef,
{
order_by => \ "SUBSTR(me.number, 1, 2) || SUBSTR(me.number, -2, 2) DESC"
}
);
You need to extract additional columns from the result set which are equal to the value of the function that you want to sort by. Then you can just put those columns in an order_by clause as normal
This assumes that your document number field is called docnum. It fetches all the columns from Table plus the two substrings of docnum called docnum1 and docnum3
my $rs = $schema->resultset('Table')->search(undef,
{
'+select' => [
{ substr => [ 'docnum', 1, 2 ], -as => 'docnum1' },
{ substr => [ 'docnum', -2 ], -as => 'docnum3' },
],
order_by => [ { -desc => 'docnum1' }, { -desc => 'docnum3' } ],
}
);

Sorting a Hash of Hashes of Array of Arrays in Perl

I made a hash (%locus) to organise my data, when I print it with Data::Dumper it shows the data structure:
'locus8 >9.2668516.276570.GABA3.1.54.6586237.218516.2718570 74280 74440 locus8'
=> {
'3 70.75 0.995018 -1.89 -' => [
[
'window10', 'locus8',
'>9.2668516.2768570.GABA3.1.54.6586237.2718516.2718570', '74280',
'74400', '-',
'3', '120',
'55.87', '-17.41',
'-11.92', '-10.60',
'-1.32', '1.57',
'-1.58', '0.68',
'2.76', '0.995018'
],
[
'window11', 'locus8',
'>9.2668516.276570.GABA3.1.54.6586237.218516.2718570', '74320',
'74440', '-', '3', '120', '70.75', '-22.97', '-17.28', '-15.07', '-2.21',
'-1.89', '0.75', '1.95', '0.976184'
]
]
},
How Can I sort the internal arrays by the last element in the arrays $_[17]?
[
[
'window11', 'locus8',
'>9.+.2668516.2768570.GABA3.1.54.6586237.2718516.2718570', '74320',
'74440', '-',
'3', '120',
'70.75', '-22.97',
'-17.28', '-15.07',
'-2.21', '1.55',
'-1.89', '0.75',
'1.95', '0.976184'
],
[
'window10', 'locus8',
'>9.+.2668516.2768570.GABA3.1.54.6586237.2718516.2718570', '74280',
'74400', '-',
'3', '120',
'55.87', '-17.41',
'-11.92', '-10.60',
'-1.32', '1.57',
'-1.58', '0.68',
'2.76', '0.995018'
],
]
I want to maintain the data structure but, with the 'internal arrays' organised. Thanks in advance.
I just try guess what you are looking for because you are not clear what you want.
for my $outher (values %hash) {
for my $inner (values %$outher) {
#$inner = sort {$a->[17] <=> $b->[17]} #$inner;
}
}

perl hash ref operator is working weirdly

I m having a single array ref consisting a list of hash refs.
Here is the code:
use strict;
use warnings;
my $arrayRef = [
{
'URL' => 'http://example.com/1.jpg',
'ORD' => '1',
},
{
'URL' => 'http://example.com/2.jpg',
'ORD' => '2',
},
{
'URL' => 'http://example.com/3.jpg',
'ORD' => '3',
},
];
print $arrayRef->[0]->{URL},"\n"; # http://example.com/1.jpg
print $arrayRef->[0]{URL},"\n"; # http://example.com/1.jpg
Since it is an hash ref i am using -> operator to get a value, but i m getting the same value without using the -> operator is it fine?
It's valid, a kind of syntactic sugar, and it refers to the same item. The Perl references tutorial writes about this