Re: Text parsing - Perl

This is a discussion on Re: Text parsing - Perl ; Newsgroups: comp.lang.perl.moderated From: Walt Mankowski <waltman@pobox.com> Subject: Re: Text parsing References: <249c32c8.0312221044.38d71e5c@posting.google.com> <3FE7E9DD.7010905@email.mot.com> Reply-To: waltman@pobox.com Message-ID: <slrnbuoog3.un2.waltman@waltman.dnsalias.org> User-Agent: slrn/0.9.8.0 (Linux) Path: waltman.dnsalias.org!news Date: Fri, 26 Dec 2003 11:29:55 -0500 On 2003-12-23, Mishra Rohit-R53658 <r53658@email.mot.com> wrote: > > Hi puchi, > ...

+ Reply to Thread
Results 1 to 2 of 2

Re: Text parsing

  1. Default Re: Text parsing

    Newsgroups: comp.lang.perl.moderated
    From: Walt Mankowski <waltman@pobox.com>
    Subject: Re: Text parsing
    References: <249c32c8.0312221044.38d71e5c@posting.google.com> <3FE7E9DD.7010905@email.mot.com>
    Reply-To: waltman@pobox.com
    Message-ID: <slrnbuoog3.un2.waltman@waltman.dnsalias.org>
    User-Agent: slrn/0.9.8.0 (Linux)
    Path: waltman.dnsalias.org!news
    Date: Fri, 26 Dec 2003 11:29:55 -0500

    On 2003-12-23, Mishra Rohit-R53658 <r53658@email.mot.com> wrote:
    >
    > Hi puchi,
    >
    > The code for reading the directory for the files inside it would look like:
    >
    > opendir( DIR, "$path" ) || die("$!");
    > while( $file = readdir(DIR)){
    > chomp($file);
    > if( $file !~ m|\.$| ){
    > # $file contains only the files inside the directory $path
    > .....
    > .....
    > }
    > }
    >
    > Hope it helps


    I don't think it'll help much, since the output of readdir in scalar
    context is just a single file. It appears that you're assuming it looks
    like the output of dos's dir command.

    Try this instead:

    opendir DIR, $dir or die "Can't open $dir: $!\n";
    while (my $file = readdir(DIR)) {
    unless (-d "$dir/$file") {
    # do something with "$dir/$file"
    }
    }
    closedir DIR;

    Also read the documentation for opendir, readdir, closedir, and -d in
    perlfunc.

    Walt

  2. Default Re: Text parsing

    I know this is late ... but here is my preferred method.

    ----- START EXAMPLE -----

    opendir(IDIR,$dir_path_string)
    or die "cannot opendir $dir_path_string: $!\n";
    @file_list_array = grep( ! /^\.{1,2}$/ , readdir(IDIR) );
    closedir(IDIR);

    ----- END - EXAMPLE -----

    Now, @file_list_array contains a list of all files in the directory
    described by $dir_path_string except those whose names are either one or
    two dots.

    I've seen people use this RegEx instead: (! /^\.+/) which will exclude
    file names that begin with one or more dots, including valid file names
    like ".rc" and ".config". I've also seen people do this: (! /^\.+$/)
    which will exclude file names made entirely of dots ... including files
    and directories named "..." - a trick used by some hackers to hide a
    directory on targetted systems.

    Walt Mankowski wrote:
    > Newsgroups: comp.lang.perl.moderated
    > Subject: Re: Text parsing
    > Date: Fri, 26 Dec 2003 11:29:55 -0500
    >
    > On 2003-12-23, Mishra Rohit-R53658 <r53658@email.mot.com> wrote:
    >
    >>Hi puchi,
    >>
    >>The code for reading the directory for the files inside it would look like:
    >>
    >>opendir( DIR, "$path" ) || die("$!");
    >>while( $file = readdir(DIR)){
    >> chomp($file);
    >> if( $file !~ m|\.$| ){
    >> # $file contains only the files inside the directory $path
    >> .....
    >> .....
    >> }
    >>}
    >>
    >>Hope it helps

    >
    >
    > I don't think it'll help much, since the output of readdir in scalar
    > context is just a single file. It appears that you're assuming it looks
    > like the output of dos's dir command.
    >
    > Try this instead:
    >
    > opendir DIR, $dir or die "Can't open $dir: $!\n";
    > while (my $file = readdir(DIR)) {
    > unless (-d "$dir/$file") {
    > # do something with "$dir/$file"
    > }
    > }
    > closedir DIR;
    >
    > Also read the documentation for opendir, readdir, closedir, and -d in
    > perlfunc.
    >
    > Walt


+ Reply to Thread

Similar Threads

  1. Help with text file parsing
    By Application Development in forum awk
    Replies: 6
    Last Post: 10-24-2007, 09:23 AM
  2. Parsing Text from a SQL Database.
    By Application Development in forum Inetserver
    Replies: 2
    Last Post: 05-19-2006, 03:03 PM
  3. Text file parsing
    By Application Development in forum basic.visual
    Replies: 6
    Last Post: 08-24-2004, 04:46 PM
  4. parsing text
    By Application Development in forum basic.visual
    Replies: 0
    Last Post: 01-30-2004, 12:14 AM
  5. Text parsing
    By Application Development in forum Perl
    Replies: 1
    Last Post: 12-23-2003, 02:08 AM