opening a file whose letter case is unknown

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default opening a file whose letter case is unknown

    Say we have a file named "ABC" , yet A B and C can be upper or lower
    case each ( 8 combinations ).

    how can one neatly open this file without preprocessing a directory
    content ?

    i would have wanted something like


    open(FILE, "ABC / ignore_case " )

    Thanks in advance

    Doron.

    Doron Stein Guest

  2. Similar Questions and Discussions

    1. #40068 [NEW]: Warning: Unknown: failed to open stream: No such file or directory in Unknown..
      From: triphius at tripslair dot com Operating system: FreeBSD 6.1 PHP version: 5.2.0 PHP Bug Type: Scripting Engine problem...
    2. No Pcase? how do I capitolize the first letter of aword, with the rest of the word lower case?
      I know I can go one way or another, Ucase(var) all upppercase, or Lcase(var) all lowercase, but how can I get a Propercase? I've seen this in VB,...
    3. #25195 [Fbk->Csd]: Failed opening phpinfo.php ... in Unknown on line 0
      ID: 25195 User updated by: mhawkins at ukeu dot com Reported By: mhawkins at ukeu dot com -Status: Feedback...
    4. #12995 [Csd->Opn]: "Failed opening ... in Unknown on line 0
      ID: 12995 User updated by: se at brainbits dot net Reported By: se at brainbits dot net -Status: Closed +Status: ...
    5. Changing case of the first letter of words in string
      I want to make the case of the first letter of all the words in a selected string to upper case. The code s/\b(\w+)/\u$1\E/g; enables me to do...
  3. #2

    Default Re: opening a file whose letter case is unknown

    On Wed, 17 Sep 2003 12:49:19 +0300, Doron Stein <dostein@cisco.com> wrote:
    > Say we have a file named "ABC" , yet A B and C can be upper or lower
    > case each ( 8 combinations ).
    >
    > how can one neatly open this file without preprocessing a directory
    > content ?
    >
    > i would have wanted something like
    >
    >
    > open(FILE, "ABC / ignore_case " )
    my $file = glob('[Aa][Bb][Cc]');
    die "No file matching [Aa][Bb][Cc]" unless defined $file;
    open(FILE, $file) or die "Unable to open $file: $!";

    Of course that will just read all the filenames in the directory and find
    the matches, and is probably inefficient since it will keep searching
    after finding a match...

    --
    Sam Holden

    Sam Holden Guest

  4. #3

    Default Re: opening a file whose letter case is unknown

    "Doron Stein" <dostein@cisco.com> wrote in message
    news:1063792099.938089@sj-nntpcache-5...
    > how can one neatly open this file without preprocessing a directory
    > content ?
    >
    > i would have wanted something like
    > open(FILE, "ABC / ignore_case " )
    On an OS like Windows, all filenames (upper case or lower case) are treated
    alike.
    So,
    open(FILE, 'ABC.txt');
    is same as
    open(FILE, 'abc.txt');

    I don't know how you can achieve the same for Unix-like systems.
    --
    "Accept that some days you are the pigeon and some days the statue."
    "A pat on the back is only a few inches from a kick in the butt." - Dilbert.


    Kasp Guest

  5. #4

    Default Re: opening a file whose letter case is unknown

    Thanks sam fro the idea ,

    and say the file name is a variable , how can i imitate the '[Aa][Bb][Cc]'

    Doron.

    Sam Holden wrote:
    > On Wed, 17 Sep 2003 12:49:19 +0300, Doron Stein <dostein@cisco.com> wrote:
    >
    >> Say we have a file named "ABC" , yet A B and C can be upper or lower
    >>case each ( 8 combinations ).
    >>
    >>how can one neatly open this file without preprocessing a directory
    >>content ?
    >>
    >>i would have wanted something like
    >>
    >>
    >>open(FILE, "ABC / ignore_case " )
    >
    >
    > my $file = glob('[Aa][Bb][Cc]');
    > die "No file matching [Aa][Bb][Cc]" unless defined $file;
    > open(FILE, $file) or die "Unable to open $file: $!";
    >
    > Of course that will just read all the filenames in the directory and find
    > the matches, and is probably inefficient since it will keep searching
    > after finding a match...
    >
    Doron Stein Guest

  6. #5

    Default Re: opening a file whose letter case is unknown

    Doron Stein <dostein@cisco.com> wrote in comp.lang.perl.misc:
    > Thanks sam fro the idea ,
    >
    > and say the file name is a variable , how can i imitate the '[Aa][Bb][Cc]'
    >
    > Doron.
    Please don't top-post, it's rude.

    (my $pat = $name) =~ s/([[:alpha:]])/[\u$1\l$1]/g;
    > Sam Holden wrote:
    > > On Wed, 17 Sep 2003 12:49:19 +0300, Doron Stein <dostein@cisco.com> wrote:
    [TOFU snipped]

    Anno

    Anno Siegel Guest

  7. #6

    Default Re: opening a file whose letter case is unknown

    > Say we have a file named "ABC" , yet A B and C can be upper or lower
    > case each ( 8 combinations ).
    >
    > how can one neatly open this file without preprocessing a directory
    > content ?
    >
    > i would have wanted something like
    >
    >
    > open(FILE, "ABC / ignore_case " )
    >
    > Thanks in advance
    >
    > Doron.
    >
    Hello Doron,

    as you will have to care for the directory's content anyway, I suggest
    to use grep and readdir:

    opendir (DIR,'.') || die ($!);
    my $filename='voId.txt';
    open (FILE, ((grep /$filename/i,readdir DIR)[0])) || die ($!);

    This will open the first file found.

    HTH,
    Dominik

    Dominik Seelow Guest

  8. #7

    Default Re: opening a file whose letter case is unknown

    Doron Stein (dostein@cisco.com) wrote on MMMDCLXIX September MCMXCIII in
    <URL:news:1063792099.938089@sj-nntpcache-5>:
    @@ Say we have a file named "ABC" , yet A B and C can be upper or lower
    @@ case each ( 8 combinations ).
    @@
    @@ how can one neatly open this file without preprocessing a directory
    @@ content ?

    What if the directory contains all of "Abc", "aBc" and "abC", which
    one do you want to open?

    Note that in general, what you want isn't possible. Most file-systems
    don't store the file names in an order that depends on the file name.
    (And there's not general API for filesystems that do). Even opening
    a file means that potentially all of the directory content will be
    processed.


    Abigail
    --
    sub f{sprintf'%c%s',$_[0],$_[1]}print f(74,f(117,f(115,f(116,f(32,f(97,
    f(110,f(111,f(116,f(104,f(0x65,f(114,f(32,f(80,f(1 01,f(114,f(0x6c,f(32,
    f(0x48,f(97,f(99,f(107,f(101,f(114,f(10,q ff)))))))))))))))))))))))))
    Abigail Guest

  9. #8

    Default Don't use glob() in a scalar context (was Re: opening a file whose letter case is unknown)

    >>>>> "Sam" == Sam Holden <sholden@flexal.cs.usyd.edu.au> writes:

    Sam> my $file = glob('[Aa][Bb][Cc]');

    Don't use glob in a scalar context, as it maintains state. Consider
    the output (on a Unix system) of:

    for (1..10) {
    my $passwd = glob '/etc/passw[d]';
    if ($passwd) {
    print "found it at $passwd\n";
    } else {
    print "not found!\n";
    }
    }

    Every other time, it will print "not found", because it didn't find
    *another* one.

    You want

    my ($file) = glob('[Aa][Bb][Cc]');

    to get the first one (if any) discarding all others.

    print "Just another Perl hacker,"

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    Randal L. Schwartz Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139