How do you print C dynamic array contents from dbx? - dbx

How do you print C dynamic array contents from dbx?
i tried print aj[2..10:1] but it didnt work

Just say "print aj"
Dbx will use dynamic information to print the array.
Are you asking about array slicing syntax in C?
You could try this:
(dbx) print a[1..1]
a[1..1] =
[1] = -17334836

Related

Create a string from a list using list comprehension

I am trying to create a string separated by comma from the below given list
['D:\\abc\\pqr\\123\\aaa.xlsx', 'D:\\abc\\pqr\\123\\bbb.xlsx', 'D:\\abc\\pqr\\123\\ccc.xlsx']
New string should contain only the filename like below which is separated by comma
'aaa.xlsx,bbb.xlsx,ccc.xlsx'
I have achieved this using the below code
n = []
for p in input_list:
l = p.split('\\')
l = l[len(l)-1]
n.append(l)
a = ','.join(n)
print(a)
But instead of using multiple lines of code i would like to achieve this in single line using a list comprehension or regular expression.
Thanks in advance...
Simply do a
main_list = ['D:\\abc\\pqr\\123\\aaa.xlsx', 'D:\\abc\\pqr\\123\\bbb.xlsx', 'D:\\abc\\pqr\\123\\ccc.xlsx']
print([x.split("\\")[-1] for x in main_list])
OUTPUT:
['aaa.xlsx', 'bbb.xlsx', 'ccc.xlsx']
In case u want to get the string of this simply do a
print(",".join([x.split("\\")[-1] for x in main_list]))
OUTPUT:
aaa.xlsx,bbb.xlsx,ccc.xlsx
Another way to do the same is:
print(",".join(map(lambda x : x.split("\\")[-1],main_list)))
OUTPUT:
aaa.xlsx,bbb.xlsx,ccc.xlsx
Do see that os.path.basename is OS-dependent and may create problems on cross-platform scripts.
Using os.path.basename with str.join
Ex:
import os
data = ['D:\\abc\\pqr\\123\\aaa.xlsx', 'D:\\abc\\pqr\\123\\bbb.xlsx', 'D:\\abc\\pqr\\123\\ccc.xlsx']
print(",".join(os.path.basename(i) for i in data))
Output:
aaa.xlsx,bbb.xlsx,ccc.xlsx

How to print more variables into file?

I know how to print one value of variable but I have a problem with more variables into one line.
file = open("values","w+")
file.write(str(q+q_krok+omega+omega_krok+e+e_krok))
The desired files values:
1-8-9-9-6-6
I would like to print values of 6 variables into file and between them put some value, for instance -. Thank you
Put the values into a string, then simply write that string to file.
values = <whatever your values are, as a string>
with open(“values.txt”, “w”) as f:
f.write(values)
If you have a list of values, you could create the string by using a join statement.
val_list = [1, 2, 3, 4, 5]
values = '-'.join(val_list)
If you have a specific set of values stored in different vars, you could use an f-string.
values = f'{val1}-{val2}-{val3}-{val4}'
Try doing it this way:
li = [q,q_krok,omega,omega_krok,e,e_krok]
values = '-'.join(li)
with open("values_file", "w") as f:
f.write(values)
You can even do it this way:
file = open("values_file","w+")
file.write(values)
You can have the values into a list, like:
items = [1,8,9,9,6,6]
with open('test.txt, 'r') as f:
for elem in items[:-1]: -- for each element instead of last
print(elem, end="-") -- print the value and the separator
if (len(items) > 1):
print(items[-1]) -- print last item without separator
A very well-made tutorial about reading/writing to files in python can be watched here:
https://www.youtube.com/watch?v=Uh2ebFW8OYM&t=1264s

Perl - basic STDIN issue

I'm now with Perl.
i have the following code which the purpose is to extract the software name
by text parsing.
the software name in this case is "ddd" :
print "Please provide full installation path (Ex:/a/b/c/ddd)\n";
my $installPath = <STDIN>;
#going to extract software name
my #soft = split '/', $installPath;
my $softName = print "#soft[4]\n";
print "$softName\n";
but,
instead of getting "ddd" as software name i got:
ddd
1
i don't understand from where the '1' comes from?
Thanks for the help.
The error comes from this:
my $softName = print "#soft[4]\n";
# ^^^^^
The function print returns 1 (true) when it succeeds, which it does here. The 1 is assigned to your variable, which you then print.
print "$softName\n";
Short recap:
my $installPath = <STDIN>; # "/a/b/c/ddd"
my #soft = split '/', $installPath; # 5th element is "ddd"
my $softName = print "#soft[4]\n"; # this prints "ddd", but "1" is returned
# ^^^^^ print returns 1, which is assigned to $softName
print "$softName\n"; # "1" is printed
What you want is:
my $softName = $soft[4];
Which is just taking the 5th element of the array. You should use $ and not # when referring to a single element. You can use # when referring to a slice, multiple elements.
A better way to do what you are trying to do is using File::Basename:
use File::Basename;
my $softName = basename($installPath);
File::Basename is a core module in Perl 5.
my $softName = print "#soft[4]\n"; is a bad way of treating an array, and this is what is causing the issue.
When referencing an array as a whole, then the # should be used. What you have done here by referencing #soft[4], you do point at a particular value in the array, but you are still referring to it in an array context, and since $softName is a scalar that only wants one single value, perl tries its best to figure out what you want, since you want nothing like it at all. To make it clear to perl that you are referencing a specific item in the array and not the array as a whole, use $ instead. Perl will understand since you also specify [4].
In addition, what is being assigned to $softName is not that array value, but the result of the print which is the status code (this is where the "1" comes from).
To correct your code, change that line to:
my $softName = $soft[4];

getting a cell array of string into a matrix or table Matlab

I'm gathering information from calculations performed on some data and stored into arrays. I also have some info about these data coming from a text file which now and then contains strings.
The strings from the text files got saved into a {} cell array of strings such as:
strings={'s1' 's2' 's3'};
a=[1 2 3]
What the strings and arrays contain is generated based on a few conditionals from the data present in the text file as well as some data I have in matlab through a loop doing things like that:
srings{e}=blablahFromSomewhere{e}
a(e)=otherNumericalBlahBlahFromSomwehre(e+6)
Ultimately I want to joint this into a table. I would normally do this:
T=[a(:) strings(:)]
But I'm facing the following error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Can anyone help? I don't really want to transform the strings into integers because the content of the string is handier to have in the output in running the analysis.
Thanks :)
Code
strings={'s1' 's2' 's3'};
a=[1 2 3];
outputfile = 'output.txt';
%%// Code to horziontally concatenate to result in a Nx2 cell array
out = [num2cell(num2str(a,'%d')') strings']
%%// Write to outputfile - Method 1
out = out';
fid = fopen(outputfile,'w');
fprintf(fid, '%s\t%s\n', out{:});
fclose(fid);
%%// Write to outputfile - Method 2
%%// Create a text file and clear it out of any content. This is needed, as otherwise
%%// XLSREAD was initializing CSV files with weird characters
%% dlmwrite(outputfile,'');
%%// Write to CSV file using XLSREAD
%xlswrite(outputfile,out)
%%// Verify
type(outputfile)
Output
out =
'1' 's1'
'2' 's2'
'3' 's3'
1 s1
2 s2
3 s3
It's a little unclear what you want, but it would seem to be:
T = table(a(:), strings(:));
Assuming I'm reading the documentation for table correctly.
Or, for a cell array:
C = [num2cell(a(:)) strings(:)];
If you want to obtain a char array:
aux = num2str(a(:));
aux = mat2cell(aux,ones(1,size(aux,1)),size(aux,2));
T = cell2mat([aux strings(:)]);
The result is a 2D char array (leading spaces are introduced if needed):
T =
1s1
2s2
3s3

How do I print a hash in perl?

How do I print $stopwords? It seems to be a string ($) but when I print it I get: "HASH(0x8B694)" with the memory address changing on each run.
I am using Lingua::StopWords and I simply want to print the stop words that it's using so I know for sure what stop words are there. I would like to print these two a file.
Do I need to deference the $stopwords some how?
Here is the code:
use Lingua::StopWords qw( getStopWords );
open(TEST, ">results_stopwords.txt") or die("Unable to open requested file.");
my $stopwords = getStopWords('en');
print $stopwords;
I've tried:
my #temp = $stopwords;
print "#temp";
But that doesn't work. Help!
Last note: I know there is a list of stop words for Lingua::StopWords, but I am using the (en) and I just want to make absolute sure what stop words I am using, so that is why I want to print it and ideally I want to print it to a file which the file part I should already know how to do.
$ doesn't mean string. It means a scalar, which could be a string, number or reference.
$stopwords is a hash reference. To use it as a hash, you would use %$stopwords.
Use Data::Dumper as a quick way to print the contents of a hash (pass by reference):
use Data::Dumper;
...
print Dumper($stopwords);
to dereference a hashref :
%hash = %{$hashref}; # makes a copy
so to iterate over keys values
while(($key,$value)=each%{$hashref}){
print "$key => $value\n";
}
or (less efficient but didactic purpose)
for $key (keys %{$hashref}){
print "$key => $hashref->{$key}\n";
}
Have a look at Data::Printer as a nice alternative to Data::Dumper. It will give you pretty-printed output as well as information on methods which the object provides (if you're printing an object). So, whenever you don't know what you've got:
use Data::Printer;
p( $some_thing );
You'll be surprised at how handy it is.
getStopWords returns a hashref — a reference to a hash — so you would dereference it by prepending %. And you actually only want its keys, not its values (which are all 1), so you would use the keys function. For example:
print "$_\n" foreach keys %$stopwords;
or
print join(' ', keys %$stopwords), "\n";
You can also skip the temporary variable $stopwords, but then you need to wrap the getStopWords call in curly-brackets {...} so Perl can tell what's going on:
print join(' ', keys %{getStopWords('en')}), "\n";

Resources