Você está na página 1de 6

CHAPTER 10 : SORTING AND INDEXING

10.1 Introduction
Visual Foxpro has two commands for organizing records in a table namely Sort and Index. We can
perform Sorting and Indexing tasks at the command level from Command Window or through the
menu.

10.2 Sort
Sorting means grouping records in a particular order say ascending or descending order based on a
key field of the records.

Ascending order means that the names in the record of a table will be arranged beginning with the
letter A at the top and Z at the bottom. On a numeric field, the smallest number comes first and on a
date field, the record with the earliest date comes first.

Descending order is just the reverse of it. This means, that names in a table will be arranged with the
letter Z at the top and A at the bottom. This is particularly useful for date fields, where you often
want the most recent date to come first.

Sorting helps in locating required records such as name of subscribers equally.


Sort command Format:
Sort To <new file> ON <field/A/B/C>,<fields…>

10.3 Index
Indexing is a process of arranging records in a particular order. In indexing, original record numbers
do not change.
If the file is indexed by menu name, than the program could find a name very quickly. The index
would list just the names in alphabetical order and would contain pointers showing where each name
was in the actual table. It is possible to search this sort of ordered list very quickly and retrieve
records from large tables without a noticeable delay.

10.3.1 Types of Index


Visual FoxPRo lets to create two major types of index files. They are:

a. Single Index Files: Visual FoxPro version 1 supported only one type of index, the Single Index
file. Each index was kept in a separate file, with the extension .IDX. If the index file is kept opened,
they automatically get updated regardless of the operation performed.

Single Index files are of two types:

1. Compact: The compact index files are faster to access and occupy less disk space due to reduced
size and this makes more efficient.
Syntax: Use <Table>
Index On <expr> To <.idx file>
Compact

2. Non-compact: For creating non compact single index file:

Syntax: Use <Table>


Index On <expr> To <.idx file>

b. Compound Index Files: Compund index are identified by the .CDX extension. Unlike the IDX
files, each of which can hold only one index, compound Index Files can hold multiple indexes in
single file, which are identified by names called tags. The exact number of tags you can include in a
.CDX index file is limited only by the available main memory of the machine you are using.
Compound Index Files are of two types:

1. Structural: One special type of compound index file is called Structural Compound Index, which
is opened automatically whenever you open a table.

A structural compound index is automatically given the same name as the table file with the
extension .CDX.
To create a compound structural index file:
Syntax: Use <Table>
Index On <expr> TAG < tagname>

2. Independent: Compound Indexes other than Structural Compound Index are called Independent
Compound Indexes. These can have any name with extension .CDX. However, these files cannot be
given the same name as the table.

To create a compound independent index file:


Syntax: Use<Table>
Index On <expr> Tag <tagname>
Of <. CDX file>

10.4 Sorting Vs Indexing

• An Index file takes less disk space than a Sort file.

• The Index command works faster than Sort command.

• Index command also allows you to add new records keeping original order of records intact.

• Sorting creates a new .dbf(database), indexing creates . idx or .cdx file.


10.5 Setting a Filter Command
Specifying, or setting a filter condition is an efficient way of selecting a group of records from a
table. After you set a filter, you need not worry about adding the criterion to each command. Records
that do not meet your criteria are filtered out indefinitely, until you remove the filter.
Syntax: Set Filter To < logical expression>

10.5.1 Creating a Filter Condition


To create a filter condition:

Open the table in the Browser window and click Table menu. Choose Properties to open the Work
Area Properties dialog box.

Click on the Data filter button

Double-click on the field to be affected.

Type the remainder of the expression, or choose an operator from the Logical drop-down list, choose
the type of string from the String drop-down list and type the condition.

To verify that the expression is valid, click on verify.

Click OK button to return to the Work Area Properties dialog box.

10.5.2 Changing a Filter Condition


To change the filter condition:

Open the Expression Builder dialog Box again

In the fields list box, Double-click on the desired field.

Type the new expression: or choose the desired parameters from the Functions drop- down lists and
type the new condition.

Click OK.

10.5.3 Removing a Filter Condition


To remove the filter condition:

Select Table menu and choose Properties option. The Work Area Properties box will be displayed.

Click all fields in the work area radio button below the Allow across to box.

Click Field Filter…button. A field Picker dialog box appears.

Select Remove all Button to move the selected fields back to All fields box.
Click OK twice.

10.6 Use of Macro(s)


Macro substitution is a programming technique used in command files. The macro is just like
ordinary memory variable but has an ampersand(&) in front of it namely, & NAME, &FLD etc.
When Visual Foxpro encounters a macro in a command file, it replaces a macro with the contents of
the memory variable.

10.7 Memo Field Handling

A memo field can store any type of data namely, keyboard symbols, graphics, or special symbols.
Memo field can also store data such as scanned image, digitized sound, binary objects, executable
programs and any other data as well. The only limitations in using Memo fields are the amount of
disk space available in the computer. Dat stored in Memo fields do not form part of the database and
are not stored in the file with extension. DBF.Foxpro stores them in a separate file with extension.
DBF.Foxpro stores them in a separate file with the same name as original database file but with an
extension of ,FPT.

To enter data in Memo field, move the cursor to the memo field and press Ctrl+PgDn and click
memo field with a mouse This activates the Visual FoxPro editor. To view information type LIST at
the command window to view records in the currently created file.

Working with command Window

 Command window is used to enter the commands to create, modify, display, list, edit, sort
etc.
 CREAT A NEW TABLE
 Create <tablename>
 To insert records
 Insert or Append
 To modify the structure of the table
 Modify Structure <tablename>
 Or
 Modi struct <tablename>

• APPEND To display the table in Append mode


Use student
Append
• EDIT or CHANGE To display the table in edit mode

Edit 5 (It display record 5 to edit)


Edit fields name ,roll_no
Or
Change fields name ,roll_no
or edit for name=“john”

• BROWSE To display table in Browse mode

Use student
Browse
Or
Browse fields name,roll_no

• REPLACE To change the contents in more than one record

• DELETE AND RECALL

Delete record record_number


Or
Goto record_number

Note: Delete comand puts a physical marker to the record, which we want to delete.Records
remain in the table and can be called by RECALL command

• Recall record number


Once the records have been recalled then the physical marker from the record will be
removed.

Recall all : To recall all the records

• PACK: It permanently deletes the records marked for deletion from the table

• ZAP: ZAP command is used to delete or remove all the records from the current table

This is equivalent to delete all followed by PACK after entering ZAP records are lost forever and
cannot be recalled anyway.

DISPLAY AND LIST

Both are used to view the records when working with command window.

DISPLAY command shows the contents of the current directory or file on the screen , unless you
specify the number of records to be displayed whereas LIST command display the contents of the
entire table on the screen

Use employee

List name, city

Example:
List all
List for city=“DELHI”
Use employee
Display
Or
Display all
or
Display next 4

Questions:
Q1. What is sorting?
Q2. What is indexing?
Q3. Differentiate between:

a. Sorting vs. Indexing

b. Ascending sorting vs Descending sorting

Q4. Explain Set Filter Condition.

Você também pode gostar