-
Notifications
You must be signed in to change notification settings - Fork 3
Data::StaticTable::Query
Since StaticTable is immutable, a helper class to perform searches is provided. It can contain generated indexes. If an index is provided, it will be used if a search is performed.
You can use hash-like keys, to get a specific index for a column
$Q1<Column1>
$Q1{'Column1'}
Both can get you the index (the same you could get by using generate-index
in a StaticTable
).
You need to specify an existing StaticTable
to create this object. Optionally you can pass a list with all the column names you want to consider as indexes.
Examples:
my $q1 = Data::StaticTable::Query.new($t); #-- No index at construction
my $q2 = Data::StaticTable::Query.new($t, 'Address'); #-- Indexing column 'Address'
my $q3 = Data::StaticTable::Query.new($t, $t.header); #-- Indexing all columns
If you don't pass any column names in the constructor, you can always use the method add-index
later
Returns a representation of the StaticTable::Query object. Can be used for serialization.
Note: This value will contain the complete StaticTable for this index.
Returns the names of the columns indexed. The order can not be guaranteed. Use .sort
if you need the same order.
Returns the values indexed.
Returns the hash of the same indexes in the Query
object.
method grep(Mu $matcher where { -> Regex {}($_); True }, Str $heading, Bool :$h = True, Bool :$n = False, Bool :$r = False, Bool :$nr = False, Bool :$nh = False)
Allows to use grep over a column. Depending on the flags used, returns the resulting row information for all that rows where there are matches. You can not only use a regxep, but a Junction
of Regex
elements.
Examples of Regexp and Junctions:
# Get the rownumbers where the column 'A' contains '9'
my Data::StaticTable::Position @rs1 = $q.grep(rx/9/, "A"):n;
# Get the rownumbers where the column 'A' contains 'n' and 'e'
my Data::StaticTable::Position @rs2 = $q.grep(all(rx/n/, rx/e/), "A"):n;
When you use the flag :n
, you can use these results later with the method take
Similar to the default grep method, this contains flags that allows you to receive the information in various ways.
Consider this StaticTable and its Query:
my $t = Data::StaticTable.new(
<Countries Import Tons>,
(
'US PE CL', 'Copper', 100, # Row 1
'US RU', 'Alcohol', 50, # Row 2
'IL UK', 'Processor', 12, # Row 3
'UK', 'Tuxedo', 1, # Row 4
'JP CN', 'Tuna', 10, # Row 5
'US RU CN', 'Uranium', 0.01 # Row 6
)
);
my $q = Data::StaticTable::Query.new($t)
:n
-
Returns only the row numbers.
This is very useful to combine with the
take
method.my @a = $q.grep(all(rx/US/, rx/RU/), 'Countries'):n; # Result: The array (2, 6)
:r
-
Returns the rows, just data, no headers
my @a = $q.grep(all(rx/US/, rx/RU/), 'Countries'):r; # Result: The array # [ # ("US RU", "Alcohol", 50), # ("US RU CN", "Uranium", 0.01) # ]
:h
-
Returns the rows as a hash with header information
This is the default mode. You don't need to use the
:h
flag to get this resultmy @a1 = $q.grep(all(rx/US/, rx/RU/), 'Countries'):h; # :h is the default my @a2 = $q.grep(all(rx/US/, rx/RU/), 'Countries'); # @a1 and @a2 are identical # Result: The array # [ # {:Countries("US RU"), :Import("Alcohol"), :Tons(50)}, # {:Countries("US RU CN"), :Import("Uranium"), :Tons(0.01)} # ]
:nr
-
Like
:r
but in a hash, with the row number as the keymy %h = $q.grep(all(rx/US/, rx/RU/), 'Countries'):nr; # Result: The hash # { # "2" => $("US RU", "Alcohol", 50), # "6" => $("US RU CN", "Uranium", 0.01) # }
:nh
-
Like
:h
but in a hash, with the row number as the keymy %h = $q.grep(all(rx/US/, rx/RU/), 'Countries'):nh; # Result: The hash # { # "2" => ${:Countries("US RU"), :Import("Alcohol"), :Tons(50)}, # "6" => ${:Countries("US RU CN"), :Import("Uranium"), :Tons(0.01)} # }
Creates a new index, and it will return a score indicating the index quality. Values of 1, or very close to zero are the less ideals.
Nevertheless, even an index with a score 1 will help.
Example:
my $q1 = Data::StaticTable::Query.new($t); #-- Creates the query object and ...
$q1.add-index('Address'); #-- indexes the column 'Address'
When an index is created, it is used automatically in any further grep
calls.