First, let me thank you for taking your time to take a look at my question.
I'm studying COBOL and I've been giving an exercise where I have to create a variable that has a number of cities from a country, I know how to create this with the hierarchy thing like:
01 COUNTRY.
02 CITY-A PIC A(5) VALUE "TOKYO".
02
02
etc, etc.
The issue here is that I somehow need these values to be able to be referenced by their position. For example, I should be able to reference the CITY-A "TOKYO" by a number. Is there any way to do this? I just can't seem to figure this out.
Any help would be greatly appreciated.
Thanks again!
You need to define your table of cities. Continuing in the style you have shown, here is an example with VALUE clauses.
01 CITY-TABLE.
05 FILLER PIC X(30) VALUE "TOKYO".
05 FILLER PIC X(30) VALUE "KYOTO".
05 FILLER PIC X(30) VALUE "AIZUWAKMATSU".
... another 47 of these
Note that all the items must be the same length. "TOKYO" is five characters plus 25 trailing spaces. "AIZUWAKMATSU" is more characters, and fewer trailing spaces, but still 30 bytes. If you define your cities with fields of different lengths, you will not be able to reference them "by a number".
You will then need to use REDEFINES to give a different mapping of the data, in this case to give it a name which can be used for different occurrences in the table.
01 FILLER REDEFINES CITY-TABLE.
05 CITY-NAME PIC X(30) OCCURS 50 TIMES.
With your data-structure defined, you can try using it.
MOVE "TOKYO" TO CITY-NAME
Actually, you can't do that. The compiler will not let you. There are 50 CITY-NAME elements, and the compiler requires that you tell it which one you want to use, any time you use CITY-NAME.
To access an element in a table, you need to use a subscript.
A subscript follows its data-name ("qualified" if necessary), and is enclosed in brackets/parentheses.
MOVE "TOKYO" TO CITY-NAME ( 1 )
Now that will compile. It is using a numeric integer literal as a subscript. Not useful in every case, as often we will be "looping" to use a table.
MOVE "TOKYO" TO CITY-NAME ( some-name )
Here some-name can be data defined by the programmer (as a numeric integer) or as an "index", a numeric integer which the compiler will manage.
To establish an "index", you code INDEXED BY some-name on the item containing the OCCURS clause.
If you use a data-name as a subscript, it is just a data-name, and you are largely unlimited with the COBOL Verbs you can use with it.
If you use an index-name (the INDEXED BY) you can only change the value of the index-name with SET, PERFORM and SEARCH.
PERFORM VARYING some-name FROM 1 BY 1
UNTIL input-byte ( some-name ) EQUAL TO SPACE
OR some-name GREATER THAN 10
...
END-PERFORM
That is not such a good loop, but it is the sort of thing which will get you started.
Once complete, some-name will either be 11 (space not found) or it will be set to the value of the table which matched the first space in the data.
In the above PERFORM, some-name could either be an index-name or a data-name. The results will be the same, what the compiler generates will be different.
However, as a beginner, you should not need to be concerned with the differences between using a data-name as a subscript and using an index-name as a subscript. Your results will be the same, the generated code will be different. Leave it at that until you have some more experience.
You can have multi-dimensional tables. You can "offset" a subscript by a positive or negative amount. There is a hierachy of use for subscripts in the case of performance. There is a whole lot of understanding before you can accurately make use of "one being faster than another" (you can easily code the fastest access, and then loose much more than you save by doing the loop in a dumb way). There is also an index data item.
All of those things can be for later. Walk well first.
A final confusion (there are lots of confusions with subscripting) is there is reference-modification. Which looks a bit like using a subscript, but isn't, but can be used as a tortuous (from the point of view of later understanding) method of accessing a textual table.
MOVE "TOKYO" CITY-TABLE ( 1 : 30 )
Note that the table-name, rather than the entry-name, is being used. The colon (:) tells that it is reference-modification. Before the colon is the start-position, after the colon is the length. Both the start-position and the length can be data-names (but not index-names).
However,
MOVE CITY-TABLE ( VAR1 : 10 )
A typical sloppy usage of reference-modification, leaves the reader wondering what is being done with that data.
A subscripted item can be reference-modified, but again that is for later.
Related
I am examining the effect of A on B. A is exposure variable and B is outcome variable.There are almost 16777 records in the dataset. Out of which 21 records have missing value in A.
A is a binary variable with 1 and 2, and my director ask me to treat missing value as a value. Now, A has three value:1,0 and missing. however, i think missing value is scare and random. Shall i remove the observation with missing value?
I know gdb has several means of exploring data, some of them quite convenient. However, I cannot combine them to get that I need/want. I would like to display some custom string based on the first n values of a big array starting at <PT_arr>, and the last m values of the same array at a distance (in this case) 4096. Looking something like this:
table beginning:
0x804cfe0 <PT_arr>: 0x00100300 0x00200300 0x00300300 0x00400300
table end:
0x804cfe0 <PT_arr+4064>: 0x00500300 0x00600300 0x00700300 0x00800300
printf let's me add custom text (like table beginning)
the examine x gives me that nice alignment, let's me read many elements and group them by byte, words, etc; and shows addresses at the left (which is ideal for my case).
x aligns the content of regions of memory in an easy to read manner with the size and unit parameters. (what I want)
display is constantly printing. (what I want).
The issue with display (manual), is that unlike examine x (manual) it doesn't have a size or unit parameter.
Is there a way to accomplish that?
Thanks.
I understood the process to shorten the URL with base 62 at How do I create a URL shortener?.
Steps given are
Think of an alphabet we want to use. In your case, that's [a-zA-Z0-9]. It contains 62 letters.
Take an auto-generated, unique numerical key (the auto-incremented id of a MySQL table for example).
For this example, I will use 12510 (125 with a base of 10).
Now you have to convert 12510 to X62 (base 62)
My question is why not just create unique numerical key and return it ? What is the advantage of concerting numerical key > Base 62 > then Finally some alphanumeric number ?
Is it because final alphanumeric number will be much smaller than unique numerical key ?
Yes. The idea is to make it short and usable in a URL. A number in base 62 will use fewer characters than the same number in base 10. Notice also that URL shorteners use short hosts, such as g.co.
I can see you understand that, yes, a number written in base 62 takes less characters than a number in base 10 just like a number in base 10 takes less characters than a number in base 2 (e.g. 0101 is 3 characters longer than just '5').
So, I'll answer specifically "Why".
Sometimes a link is shortened to be more visually pleasing. A company worried about their public perception likely doesn't want their links to look like an error code due to how long they are so they resort to shortening. That's why some url shortening services allow you to add your own "vanity url" which customizes the domain name, so that a link can be shortened and branded.
Other times a link is shortened to minimize character count when working with constraints, like Twitter. For example, at my company we shortened the links in our automated Twilio messages because SMS messages that contain more than 160 characters are technically 2 concatenated messages so it is more expensive to send.
And finally if the link is being shared through a medium that cannot be directly clicked on (e.g. verbally, on paper), making it shorter makes it much easier to type into an address bar manually. (Imagine trying to type the url to this SO question when someone is reading it to you.) I assume this is also at least partially why the base used for these links usually stop at around 62. If you start including other arbitrary characters to higher the base and consequentially make the link marginally shorter, it'll become harder to communicate, read and type. ("domain.name/5omeC0d3" vs "domian.name/🈲}♠"
I am wanting to solve this problem, but am kind of unsure how to correctly structure the logic for doing this. I am given a list of user names and I am told to find an extracted name for that. So, for example, I'll see a list of user names such as this:
jason
dooley
smith
rob.smith
kristi.bailey
kristi.betty.bailey
kristi.b.bailey
robertvolk
robvolk
k.b.dula
kristidula
kristibettydula
kristibdula
kdula
kbdula
alexanderson
caesardv
joseluis.lopez
jbpritzker
jean-luc.vey
dvandewal
malami
jgarciathome
christophertroethlisberger
How can I then turn each user name into an extracted name? The only parameter I am given is that every user name is guaranteed to have at least a partial person's name.
So for example, kristi.bailey would be turned into "Kristi Bailey"
alexanderson would be turned into "Alex Anderson"
So, the pattern I see is that, if I see a period I will turn that into two strings (possibly a first and last name). If I see three periods then it will be first, middle. The problem I am having trouble finding the logic for is when the name is just clumped up together like alexanderson or jgarciathome. How can I turn that into an extracted name? I was thinking of doing something like if I see 2 consonants and a vowel in a row I would separate the names, but I don't think that'll work.
Any ideas?
I'd use a string.StartsWith method and a string.EndsWith method and determine the maximum overlap on each. As long as it's more than 2 characters, call that the common name. Sort them into buckets based on the common name. It's a naive implementation, but it that's where I'd start.
Example:
string name1 = "kristi.bailey";
string name2 = "kristi.betty.bailey";
// We've got a 6 character overlap for first name:
name2.StartsWith(name1.Substring(0,6)) // this is true
// We've got a 6 character overlap for last name:
name2.EndsWith(name1.Substring(7)) // this is true
HTH!
I'm trying to read files and create a hashmap of the contents, but I'm having trouble at the parsing step. An example of the text file is
put 3
returns 3
between
3
pargraphs 1
4
3
#foo 18
****** 2
The word becomes the key and the number is the value. Notice that the spacing is fairly erratic. The word isn't always a word (which doesn't get picked up by /\w+/) and the number associated with that word isn't always on the same line. This is why I'm calling it not well-formed. If there were one word and one number on one line, I could just split it, but unfortunately, this isn't the case. I'm trying to create a hashmap like this.
{"put"=>3, "#foo"=>18, "returns"=>3, "paragraphs"=>1, "******"=>2, "4"=>3, "between"=>3}
Coming from Java, it's fairly easy. Using Scanner I could just use scanner.next() for the next key and scanner.nextInt() for the number associated with it. I'm not quite sure how to do this in Ruby when it seems I have to use regular expressions for everything.
I'd recommend just using split, as in:
h = Hash[*s.split]
where s is your text (eg s = open('filename').read. Believe it or not, this will give you precisely what you're after.
EDIT: I realized you wanted the values as integers. You can add that as follows:
h.each{|k,v| h[k] = v.to_i}