Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scanning directories for XML files and reading them

Status
Not open for further replies.

ahammad

Programmer
Sep 24, 2007
10
FR
Hello,

I would like to scan a certain directory for XML files. Then I want to take in every XML file, read it, and stores the contents as a string in an array. For example, if the directory has file1.xml, file2.xml, and file3.xml, they would all be scanned and read. Then, all the contents of file1.xml would be stored in an array as the first element:

@array[0] would contain the stuff in file1.xml
@array[1] would contain the stuff in file2.xml
etc.

This is what I have:

Code:
sub getComponents
{
	my			$dir 					= shift;
	my 			@Components		= ();
	my			@xmlFiles				= ();
	
	if ( -d $dir )
	{
		open ( COMPONENTS, $dir );
		@xmlFiles = grep ( /^(.+)\.xml$/, grep ( !/^.{1,2}$/, readdir( COMPONENTS ) ) );
		closedir ( COMPONENTS );
		
		$dir = addSlash( $dir );
		
		foreach ( @xmlFiles )
		{
			my @fileInfo = readTextFile ( $dir . $_ );
			push( @Components, \@fileInfo );
		}
	}
	
	return @Components;
}

readTextFile is another sub that has been tested. No problems there. addSlash adds a slash at the end of the directory so that the files can be opened. This is also tried and tested.

The problem is this code simply doesn't work. I don't know where to start looking.

Thanks
 
shouldn't this:
Code:
  [b]open[/b]( COMPONENTS, $dir );
be this:
Code:
  [b]opendir[/b]( COMPONENTS, $dir );
?
 
untested code:

Code:
[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]getComponents[/maroon]
[red]{[/red]
    [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$dir[/blue] = [url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url][red];[/red]
    [black][b]my[/b][/black] [blue]@Components[/blue] = [red]([/red][red])[/red][red];[/red]
    [url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]@ARGV[/blue] = <[blue]$dir[/blue]/[blue]*[/blue].xml>[red];[/red]
    [black][b]local[/b][/black] [blue]$/[/blue][red];[/red]
    [olive][b]while[/b][/olive][red]([/red]<>[red])[/red][red]{[/red]
        [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@Components[/blue],[blue]$_[/blue][red];[/red]
    [red]}[/red]
    [url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url] [blue]@Components[/blue][red];[/red]
[red]}[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I don't know what readTextFile does, but it appears to return an array, and ahammad wants the array reference pushed onto components to make an array of arrays ?

But other than that, yes, it looks like another candidate for a glob over a grep.

Code:
sub getComponents {
  my $dir        = shift;
  my @components = ();
  push @components, [ readTextFile( $_ ) ] foreach( glob( "$dir/*.xml" ) );
  return @components;
}

$components[0][0] now returns the first line of the first file.

Of course there is the issue that there is no tying of the filename to the array, which would have been better handled by a hash of arrays, keyed by the filenames.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top