Thursday, December 1, 2016

Defining a Portion of a Field

How to Define a Portion of a Field

One of the more common questions we get with respect to data and portions of data in a given field. Specifically, let's say you want to define the last four bytes of a given 20 byte field.

So with 20 bytes the last four bytes begins at byte 17:

  12345669132135689071  Data
  12345678901234567890  Byte Position
 
So starting at offset 17 for a length of 4, the data extracted should be 9071.
 >in atminfo
>list
>xeq
>IN atminfo (0) >OUT $NULL (0)
ATM-CARD-NUMBER = 12345669132135689071

IN=1, OUT=1. CPU-Sec=1. Wall-Sec=1.

>in atminfo
>def last-4,atm-card-number[17],4
>ext last-4
>list
>xeq
>IN atminfo (0) >OUT $NULL (0)
LAST-4          = 9071

IN=1, OUT=1. CPU-Sec=1. Wall-Sec=1.

So to deconstruct the define command, you define the new name associated with the field you want a portion of, and you define the starting offset and the length.

How to Add a Line Before and After a String

I was recently asked how one could add a Line Before and After a particularly String was found:
 
 This is a line.

would become:
 Before Line
 This is a line.
 After Line
Now normally in Qedit (host-based) this is extremely easy using a couple of tricks.
  ch "This is a line."Before Line~This is a line.~After Line" @
  divide "~" @
  ch "~This is a line."This is a line." @
  divide "~" @
  ch "~After Line"After Line" @
The use of the tilde character (I use this as it is typically unique) along with change and divide we end up getting a new line before and a new line after. However, the customer that was asking for this has Qedit for Windows only and Qedit server, which doesn't allow Host-based Qedit commands. Following is a script of a Qedit Scripting Language programming language, that will do just that:
-- Copyright 1996-2015 Robelle Solutions Technology Inc.
-- Version 1.10 May 21, 2015
sub create_at (line,column)

result = {};
result.line = line;
result.column = column;
return result;

endsub;

result = dialog("What string do you want to find?",1, "String to find.");
if result.button = 1 then 
   userString = result.enteredText;
endif
result = dialog("What line should be before?" ,1, "Line to add before string.");
if result.button = 1 then
   lineBefore = result.enteredText;
endif
result = dialog("What line should be after?" ,1, "Line to add after string.");
if result.button = 1 then
   lineAfter = result.enteredText;
endif


theFile = qedit.activefile;
findresult = theFile.find(string: userString);
repeat while findresult
   row = theFile.lastfoundline - 1;
   where = create_at(row,1);
   theFile.insert(at: where, text: lineBefore);
   row = theFile.lastfoundline + 1;
   where = create_at(row,1);
   theFile.insert(at: where, text: lineAfter);
   findresult = theFile.find(string: userString);
endrepeat

theFile = dialog("End of Script");

No comments:

Post a Comment