Finding the number of occurences in an array

B

Bill H

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?

opendir(BASE, "things");
@gfnames = readdir(BASE);
closedir(BASE);
 
A

A. Sinan Unur

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

I assume you meant how many files have the extension .txt.
through and comparing each one?

use strict;
use warnings;
opendir(BASE, "things");

opendir my $base_dir, 'things'
or die "Cannot open 'things' directory: $!";
@gfnames = readdir(BASE);

my @gfnames = grep { /\.txt$/ } readdir $base_dir;
closedir(BASE);

closedir $base_dir;

perldoc -f grep

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
J

Josef Möllers

Bill said:
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?

Well, simply by magic!
You use the magic wand to transform the array into a count! Entries without
the .txt extension will not be considered. Also, since you already know
which entries have the .txt extension, you just need to _count_ the others,
no comparison needed! That way, you could even sort an arbitrary large
array in constant time!

Seriously: how on earth do you think you can determine whether an item
belongs into a given class without looking at the item?
You can usually skip the first and second entry, as they are '.' and '..',
but you still have to check the rest one by one.

You could offload that out of your code by doing:

foreach (<things/*.txt>) {
push @gfnames, $_;
}
print "There are ", scalar @gfnames, " .txt-Files\n";

But that would still check each and every entry elsewhere.
 
D

Dr.Ruud

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;
 
D

Dr.Ruud

Mintcake schreef:

You must quote an appropriate part of the message that you react on.
How about:

scalar grep /\.txt/, <things/*>;

No, because the question was:

<quote>
is there an easy way of find out how many of
the entries in @gfnames contain the string ".txt"
<quote/>

From that you can assume that others things need to be done with
@gfnames. You cannot assume that the elements without ".txt" don't
matter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top