A globbing question

A

Alex

Hello,

I'm trying to figure out how to "glob" lists instead of the current
directory. For example:

my @var = File::Glob::bsd_glob('a*');
foreach my $f (@var) {print $f, "\n";}

This fragment will print all the filenames started with 'a' in the
current directory.

I need some function which doesn't depend on the file system. For
example

my @set = ('ab', 'ac', 'bc');
my @var = new_bsd_glob('a*', @set); # <---- non-existing function
with additional parameter
foreach my $f (@var) {print $f, "\n";}

It must work exactly as shell globbing, so it should print 'ab' and
'ac'.

I suspect this "new_bsd_glob" function already exist - could you
please advice where it resides?

Thanks
Alex
 
A

Alex

Hello,

I'm trying to figure out how to "glob" lists instead of the current
directory. For example:

my @var = File::Glob::bsd_glob('a*');
foreach my $f (@var) {print $f, "\n";}

This fragment will print all the filenames started with 'a' in the
current directory.

I need some function which doesn't depend on the file system. For
example

my @set = ('ab', 'ac', 'bc');
my @var = new_bsd_glob('a*', @set); # <---- non-existing function
with additional parameter
foreach my $f (@var) {print $f, "\n";}

It must work exactly as shell globbing, so it should print 'ab' and
'ac'.

I suspect this "new_bsd_glob" function already exist - could you
please advice where it resides?

Thanks
Alex

I've already found the Text::Glob !!

Thanks
Alex
 
J

John W. Krahn

Alex said:
I'm trying to figure out how to "glob" lists instead of the current
directory. For example:

my @var = File::Glob::bsd_glob('a*');
foreach my $f (@var) {print $f, "\n";}

This fragment will print all the filenames started with 'a' in the
current directory.

I need some function which doesn't depend on the file system. For
example

my @set = ('ab', 'ac', 'bc');
my @var = new_bsd_glob('a*', @set); # <---- non-existing function
with additional parameter
foreach my $f (@var) {print $f, "\n";}

It must work exactly as shell globbing, so it should print 'ab' and
'ac'.

It sounds like you want something like:

my @var = new_bsd_glob('{a*,ab,ac,bc}')



John
 
J

Jim Gibson

Alex said:
Hello,

I'm trying to figure out how to "glob" lists instead of the current
directory. For example:

my @var = File::Glob::bsd_glob('a*');
foreach my $f (@var) {print $f, "\n";}

This fragment will print all the filenames started with 'a' in the
current directory.

I need some function which doesn't depend on the file system. For
example

my @set = ('ab', 'ac', 'bc');
my @var = new_bsd_glob('a*', @set); # <---- non-existing function
with additional parameter
foreach my $f (@var) {print $f, "\n";}

It must work exactly as shell globbing, so it should print 'ab' and
'ac'.

I suspect this "new_bsd_glob" function already exist - could you
please advice where it resides?

Perhaps you are looking for grep:

my @var = grep( /a*/, @set );

See 'perldoc -f grep'
 
A

Alex

Perhaps you are looking for grep:

my @var = grep( /a*/, @set );

See 'perldoc -f grep'

Jim,

No, grep doesn't work for me. I need globbing, not regexes, because
many of special pattern symbols are interpreted differently. For
example, asterisk represents any number of any symbols in globbing
(like in Unix shell), but in regexes it represents a repetition of the
previous symbol or subexpression.

The Text::Glob module does what I want.

Thanks
AY
 
U

Uri Guttman

A> No, grep doesn't work for me. I need globbing, not regexes, because
A> many of special pattern symbols are interpreted differently. For
A> example, asterisk represents any number of any symbols in globbing
A> (like in Unix shell), but in regexes it represents a repetition of the
A> previous symbol or subexpression.

huh?? in regexes . is the same as * in globs. so what? glob patterns are
simplistic vs regexes which can do any globbing and more. your choice of
glob for a filter makes little sense.

uri
 
J

John W. Krahn

Uri said:
A> No, grep doesn't work for me. I need globbing, not regexes, because
A> many of special pattern symbols are interpreted differently. For
A> example, asterisk represents any number of any symbols in globbing
A> (like in Unix shell), but in regexes it represents a repetition of the
A> previous symbol or subexpression.

huh?? in regexes . is the same as * in globs. so what? glob patterns are
simplistic vs regexes which can do any globbing and more. your choice of
glob for a filter makes little sense.

ITYM: in regexes . is the same as ? in globs.



John
 
U

Uri Guttman

A> many of special pattern symbols are interpreted differently. For
A> example, asterisk represents any number of any symbols in globbing
A> (like in Unix shell), but in regexes it represents a repetition of the
A> previous symbol or subexpression.
JWK> ITYM: in regexes . is the same as ? in globs.

yeah. shows how often i use globs for filtering in perl! in shells i use
them as needed. but my point is valid. globs have very little power and
regexes can do the same with about as little syntax.

uri
 
P

Peter J. Holzer

A> many of special pattern symbols are interpreted differently. For
A> example, asterisk represents any number of any symbols in globbing
A> (like in Unix shell), but in regexes it represents a repetition of the
A> previous symbol or subexpression.

JWK> ITYM: in regexes . is the same as ? in globs.

yeah. shows how often i use globs for filtering in perl! in shells i use
them as needed. but my point is valid. globs have very little power and
regexes can do the same with about as little syntax.

One reason why you might want globs instead of regexes is that the users
are more familiar with globs. If you tell them that they can use
wildcards, there is a chance that they know what you mean. If you talk
about regexes, you'll just get blank stares.

That said, I think it is rather simple to convert (basic) globs to
regexes, and I suspect that this is just what Text::Glob does (but I
haven't checked).

hp
 
U

Uri Guttman

JWK> ITYM: in regexes . is the same as ? in globs.
PJH> One reason why you might want globs instead of regexes is that the users
PJH> are more familiar with globs. If you tell them that they can use
PJH> wildcards, there is a chance that they know what you mean. If you talk
PJH> about regexes, you'll just get blank stares.

PJH> That said, I think it is rather simple to convert (basic) globs to
PJH> regexes, and I suspect that this is just what Text::Glob does (but I
PJH> haven't checked).

i am sure i have seen formulas to convert glob patterns to regexes. the
basics are simple (if i get them correct this time :). ? is . and * is
..* and you need /s on both. but you generally don't find \n inside file
names even though it is legal! char classes are similar enough. {} of
globs is mappable to alternation. and AFAIK globs have no concept of
repeating anything, just .* stuff let alone none of the fancier tricks
and shortcuts.

so IMO the basic glob ops of * and ? are trivial to teach in regex.

uri
 
T

Tim McDaniel

JWK> ITYM: in regexes . is the same as ? in globs.

PJH> One reason why you might want globs instead of regexes is that
PJH> the users are more familiar with globs. If you tell them that
PJH> they can use wildcards, there is a chance that they know what
PJH> you mean. If you talk about regexes, you'll just get blank
PJH> stares.

Another reason may be just a legacy interface.
PJH> That said, I think it is rather simple to convert (basic)
PJH> globs to regexes, and I suspect that this is just what
PJH> Text::Glob does (but I haven't checked).

i am sure i have seen formulas to convert glob patterns to
regexes. the basics are simple (if i get them correct this time :). ?
is . and * is .*

Off the top of my head: in UNIXY shells, * doesn't match any name that
starts with ".", so if you want that behavior, ou'd have to account
for that. And if the input is a full path rather than just a member
of a directory, then ? is [^/] and * is [^/]*. (Or, instead of /,
whatever your path name delimiter is).

So Text::Glob is looking like a better choice, just because they may
have considered the edge cases and cases we haven't considered.
 

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
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top