|
PRINT CLUSTER
and
ASK CLUSTER
in SheerpowerPRINT CLUSTER
StatementThe PRINT CLUSTER
statement in Sheerpower allows you to output the
contents of a cluster array. You can choose to print all the rows in the cluster
or just the current row, depending on how you invoke the command. By default,
the rows are printed in CSV format, making it easy to export and share data directly.
Syntax:
Examples:
This prints every row in the cities
cluster.
This prints just the current row in the cities
cluster.
ASK CLUSTER
StatementThe ASK CLUSTER
statement is used to retrieve metadata about
the cluster, such as the column headers, into a variable.
The ASK CLUSTER clustername: headers x$
statement in Sheerpower
is used to retrieve the names of the fields (headers) in a cluster. The headers
are stored as a comma-separated string in the variable x$
. This
allows you to programmatically access and use the headers of a cluster, for
example, when exporting data or generating reports.
Syntax:
Examples:
This retrieves the column headers from the cities
cluster and
stores them in the string variable headers$
. The print
statement then outputs the headers.
PRINT CLUSTER
: This statement is versatile for
debugging or displaying cluster contents. When you use : all
,
it prints all rows, which is useful for reviewing or outputting the entire
dataset. Without : all
, it focuses on the current row, which is
helpful when iterating through rows one by one.
ASK CLUSTER
: The headers
keyword is
particularly useful when you need to work with column names programmatically.
By storing the headers in a variable like x$
, you can dynamically
reference or output the structure of the data in your cluster.
These commands are essential tools in Sheerpower for managing and displaying cluster data, making them powerful in scenarios involving data processing, reporting, or simply verifying the structure and content of clusters.
Sheerpower also supports loading JSON files into clusters using the cluster input statement. Below are the details and examples:
The cluster input statement allows you to read JSON files and load their data into a Sheerpower cluster. The structure of the JSON file should have a consistent number of name/value pairs for each "row". The data is loaded sequentially into the cluster without regard to the field names.
Consider a JSON file named @json_prefixed.json with the following content:
To load this JSON data into a Sheerpower cluster, use the following code:
This code will read the JSON file and load the data into the people3 cluster. Each object in the JSON array is treated as a row in the cluster.
This program demonstrates how to process an X12 file into a Sheerpower cluster:
Program Explanation:
1. Define Clusters:
The program defines a cluster named cities
to store information about cities. The cluster fields include the city name (City$
), country (Country$
), population (Population
), region (Region$
), latitude (Latitude
), and longitude (Longitude
).
2. Load Data from CSV:
The program reads the CSV file safe_world_cities.csv
into the cities
cluster. The headers 1
option indicates that the first row of the CSV file contains the column headers, which match the fields defined in the cities
cluster.
3. Print Number of Cities:
The program prints the total number of cities loaded into the cities
cluster.
4. Assign User Input:
The value of the variable a$
is assigned to mycity$
, representing the city name that the user wants to look up.
5. Search for the City:
The program searches the cities
cluster for a row where the City$
field matches mycity$
. The row number is stored in the variable row
.
6. Handle Unknown City:
If the city is not found (row = 0
), the program prints a message indicating that the city is unknown and suggests trying a different city. The loop then exits.
7. Print Population:
If the city is found, the program prints the population of the city using the sprintf
function to format the output.
8. Filter and Sort Cities:
The program starts a collect
block to filter and sort the cities
cluster. It includes only cities with a population greater than 10 million and sorts the cities by their name, converted to uppercase.
9. Print Filtered Cities:
The program iterates over the filtered and sorted cities
cluster, printing the city name, country, and population for each city.
Hide Description
|
|
Enter or modify the code below, and then click on RUN |
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |