Parsing a string into an array

Parsing a string into an array

am 16.11.2007 22:28:26 von AndrewMcHorney

Hello

I am getting a strange error 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.

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

Thanks,
Andrew


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

#
# 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];

print @file_list[$temp_index];
print "\n";

if (@file_list[$temp_index] ne "")
{
#
#
@parse_line = split(@file_list[$temp_index]);

# $line_size = scalar(@parse_line);
$line_size = 0;

if ($line_size > 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")
{
#$directory_list = @parse_line[1];
#$directory_index =$directory_index + 1;
#print "is a Directory\n";
}
else
{
##$actual_files[file_index] = @parse_line[0];
# $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;
}



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Parsing a string into an array

am 17.11.2007 23:52:31 von krahnj

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

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/