Re: Finding the number of occurences in an array



Bill H schreef:

Perl has so many quick ways of doing things, I wonder if there is an
easy way to find out how many entries in an array match a string?

if I use the following to get a directory list of the sub directory
"things" into @gfnames, is there an easy way of find out how many of
the entries in @gfnames contain the string ".txt" without going
through and comparing each one?

(variant of Sinan's reply)

use strict;
use warnings;

opendir(BASE, "things");

opendir my $base_dir, 'things'
or die "Cannot open 'things' directory: $!";

@gfnames = readdir(BASE);

my @gfnames = readdir $base_dir;


my $txt_count = grep( /\.txt/, @gfnames );

closedir(BASE);

closedir $base_dir;

--
Affijn, Ruud

"Gewoon is een tijger."
.



Relevant Pages

  • Finding the number of occurences in an array
    ... easy way to find out how many entries in an array match a string? ... the entries in @gfnames contain the string ".txt" without going through ...
    (comp.lang.perl.misc)
  • Re: Finding the number of occurences in an array
    ... easy way to find out how many entries in an array match a string? ... if I use the following to get a directory list of the sub directory ... the entries in @gfnames contain the string ".txt" without going ...
    (comp.lang.perl.misc)
  • Re: Finding the number of occurences in an array
    ... easy way to find out how many entries in an array match a string? ... the entries in @gfnames contain the string ".txt" without going through ... You use the magic wand to transform the array into a count! ...
    (comp.lang.perl.misc)
  • Re: converting bash-scripts into GUIs
    ... a window with various entries, check buttons, option buttons/menus, ... dialog is limited to shell windows (it is not a 'true' GUI in a sense). ... check buttons (for selecting (or not) optional values) ... string Y. The script takes four fixed parameters and two options: ...
    (comp.os.linux.misc)
  • Re: Clarification
    ... Sorry to keep complicating matters but the ... first two entries arent always the same character count... ... this time with comma as your delimiter will put the surname in C and the ... If I set the Text to Columns for an 8 entry string format... ...
    (microsoft.public.excel.misc)