On 7/28/2001 2:16 AM, Thomas wrote:
> What I would like to know is, how can I do a grep that is case insensitive ?
Perl's grep has no inherent case-sensitiveness or insensitiveness. It's used
to run a block/expr of code for each item of an array and return the
elements of the array for which the block/expr evaluates to true. It's the
code within the block/expr that determines whether the entire grep
expression (block and all) is case sensitive or not.
Examples:
# Get all items in @names that begin with a 'D' (case-insensitive)
@d_names = grep { /^d/i } @names;
# Get all items in @names that end with a lowercase 'r'
# Returns 'Amir' and 'Haager', but not 'PETER'
@r_names = grep { /r$/ } @names;
# Get all items in @locations that have two words and the first
# word is 'Los' (case insensitive).
# Returns 'Los Alamitos', 'Los Angeles', but not 'Loshard' or
# 'Los Montanas Azules'
@los_places = grep {
my @words = split(/\s+/);
@words == 2 and $words[0] =~ /los/i;
} @locations;
HTH
Regards,
David