develooper Front page | perl.beginners | Postings from November 2007

Re: Parsing a string into an array

Thread Previous
From:
John W . Krahn
Date:
November 17, 2007 14:53
Subject:
Re: Parsing a string into an array
Message ID:
07111714523100.28956@d66-183-63-222
On Friday 16 November 2007 13:28, AndrewMcHorney wrote:
> Hello

Hello,

> I am getting a strange error

Why do you think it is strange?  Have you read the documentation for 
the split function and do you understand it?

perldoc -f split

> where I am trying to split a string into
> an array. The string is a line obtained from doing a directory of a
> disk. The error message is:
>
> Reference to nonexistent group in regex; marked by <-- HERE in m/
> Directory of C
>
> :\Documents and Settings\Andrew\Application
>
> Data\ActiveState\KomodoIDE\4 <-- HER
> E .1/ at D:\Perl Scripts\remove_duplicate_files.pl line 56.

In a regular expression pattern \1, \2, \3, \4, etc. refer back to 
capturing parentheses in the pattern, respectively the first group, the 
second group, the third group, the fourth group, etc.  If you want to 
match a literal '\' character in a regular expression you have to 
escape it.

perldoc -f quotemeta

> The line in where it happens is the line of code with the split.

When developing your code you should include the warnings and strict 
pragmas to let perl help you find mistakes.

use warnings;
use strict;

> #
> # Find all the files
> #
> @file_list = `dir c: /S`;

Have you thought of using a module like File::Find to do this instead?


> #
> # Build the list of directories and files
> #
> $temp_index = 0;
>
> $file_index = 0;
>
> $directory_index = 0;
>
> $dir_list_size = scalar(@file_list);
>
> print $dir_list_size;
> print "\n:";
>
> while ($temp_index < $dir_list_size)
> {
>     chomp @file_list[$temp_index];

      chomp $file_list[$temp_index];

>     print @file_list[$temp_index];

      print $file_list[$temp_index];

>     print "\n";
>
>     if (@file_list[$temp_index] ne "")

      if ($file_list[$temp_index] ne "")

>     {
>        #
>        #
>        @parse_line = split(@file_list[$temp_index]);

perldoc -f split
       split /PATTERN/,EXPR,LIMIT

       split /PATTERN/,EXPR

       split /PATTERN/

       split   Splits a string into a list of strings and returns
               that list.  By default, empty leading fields are
               preserved, and empty trailing ones are deleted.


The first argument to split() is a regular expression pattern.  If you 
do not supply a regular expression pattern then split will convert 
whatever you *did* supply *to* a regular expression pattern.  So the 
line above:

        @parse_line = split(@file_list[$temp_index]);

is seen by perl as:

        @parse_line = split( /$file_list[$temp_index]/, $_ );


>        # $line_size = scalar(@parse_line);
>        $line_size = 0;
>
>        if ($line_size > 0)
>        {
>           #print @parse_line[0];

           #print $parse_line[0];

>           #print "\n";
>
>           #
>           # Determine if this is a directory and if so add it to the
>           # directory array and increment the index
>           #
>           if (@parse_line[1] eq "Directory")

           if ($parse_line[1] eq "Directory")

>           {
>              #$directory_list = @parse_line[1];

              #$directory_list = $parse_line[1];

>              #$directory_index =$directory_index + 1;
>              #print "is a Directory\n";
>           }
>           else
>           {
>              ##$actual_files[file_index] = @parse_line[0];

              ##$actual_files[file_index] = $parse_line[0];

>              # $file_size[file_index] = @parse_line[4];

              # $file_size[file_index] = $parse_line[4];

>              #$file_deleted[file_index] = 0;
>
>              #$file_index = $file_index + 1;
>              #print "not a directory \n";
>           }
>        }
>     }
>
>     $temp_index = $temp_index + 1;
> }



John
-- 
use Perl;
program
fulfillment

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About