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!

readdir and print file issue

Status
Not open for further replies.

pete1970

Programmer
Aug 16, 2007
10
US
I am looping through files in a directory and printing to csv files with:

Code:
.
.
opendir D, $opts{d} or die ...;
.
.
foreach my $file (readdir D)
{
  my ($filebase, $dirname, $ext) = fileparse($file, '\..*');
  my $csvFile = "$opts{d}\\$filebase.csv";
  open FILE_OUT, ">$csvFile" or die ...;
  .
  .
  foreach my $parent (@parents)
  {
    print FILE_OUT $parents{$parent} . "\n";
  }
  close FILE_OUT;
}

The problem is it only writes out with the first file it comes to. I originally had the program writing out to the csv files line by line inside the code and everything worked fine, then I made some necessary code changes to incorporate the %parents hash and now it still creates the other csv files but they are all 0 bytes. No matter what combination of files there are in the directory it only writes data to the first one. Any ideas?

Thanks in advance.
 
I can't tell what the problem is by the code you posted.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Please forgive the length and inefficiency.

Here is the whole thing:

Code:
#!/usr/bin/perl
use strict;

use XML::Parser;
use XML::SimpleObject;

use Getopt::Std;
use File::Basename;

my %opts = ();
getopts('d:', \%opts);
die "No Flow directory specified with -d!\n" unless exists $opts{d};

opendir D, $opts{d} or die "Error opening directory '$opts{d}': $!\n";

foreach my $file (readdir D)
{
  next unless $file =~ m#.xml$#;

  my ($filebase, $dirname, $ext) = fileparse($file, '\..*');

  my $parser = XML::Parser->new( ErrorContext => 2, Style => "Tree" );
  my $tree = XML::SimpleObject->new( $parser->parsefile( "$opts{d}\\$file" ));

  my $csvFile = "$opts{d}\\$filebase.csv";
  open FILE_OUT, ">$csvFile" or die "Could not open '$csvFile': $!\n";
  print "csvFile = $csvFile\n";

  my @parents = ();
  my %parents = ();
  my @compLib = $tree->child('stratman')->child('client_system')->child('business_area')->child('component')->child('component_lib');

  foreach my $compLib (@compLib)
  {
	if ( $compLib->attribute('type') eq 'subroutine_flow' )
	{
		my $flowRef = $compLib->child('subroutine_flow')->child('flow_structure')->child('flow_ref');
		begat( $flowRef, $compLib );
	}
  }

  foreach my $parent (@parents)
  {
	print FILE_OUT $parents{$parent} . "\n";
#	print $parents{$parent} . "\n";
  }
  close FILE_OUT;

  sub begat
  {
	sub getObject
	{
		sub getDetail
		{
			my ( $pfPo, $parentName, $indent ) = @_;
			my $pfPoName = $pfPo->attribute('name');

			if ( $pfPo->attribute('type') =~ m#split$# )
			{
				foreach my $xConn ($pfPo->child('xconnector'))
				{
					my $xOutName = $xConn->child('outcome')->attribute('name');
					$parents{$parentName} .= "$indent\"$pfPoName\",\"$xOutName\"\n";
					getDetail( $xConn->child('process_object'), $parentName, $indent . "," );
				}
			}
			elsif ( $pfPo->attribute('type') eq "outcome" )
			{
				my $pfPoPoName = $pfPo->child('process_object')->attribute('name');
				my $pfPoPoSTE  = $pfPo->child('process_object')->child('functional_process')->attribute('stategyTable_entry');
				$parents{$parentName} .= "$indent\"$pfPoPoName\",\"$pfPoPoSTE\"\n";

				if ( $pfPo->child('process_object')->child('process_object') )
				{
					getDetail( $pfPo->child('process_object')->child('process_object'), $parentName, $indent . "," );
				}
			}
			elsif ( $pfPo->attribute('type') eq "process" )
			{
				my $pfPoSTE  = $pfPo->child('functional_process')->attribute('stategyTable_entry');
				$parents{$parentName} .= "$indent\"$pfPoName\",\"$pfPoSTE\"\n";

				if ( $pfPo->child('process_object') )
				{
					getDetail( $pfPo->child('process_object'), $parentName, $indent . "," );
				}
			}
		}

		my ( $pfPo, $parent, $parentName, $compLib ) = @_;

		if ( ( $pfPo->attribute('type') eq "outcome" ) || ( $pfPo->attribute('type') eq "process") )
		{
			my $pfPoPoName = $pfPo->child('process_object')->attribute('name');

			if ( $pfPo->child('process_object')->child('process_object') )
			{
				if ( $parents{$parentName} !~ m#\"$pfPoPoName\"\n# )
				{
					$parents{$parentName} .= "\"$pfPoPoName\"\n";
				}

				getObject( $pfPo->child('process_object'), $parent, $parentName, $compLib );
			}
			elsif ( $parent->child('flow_ref') )
			{
				my $childName = $parent->child('flow_ref')->attribute('name');

				if ( $parents{$parentName} !~ m#\"$pfPoPoName\",,\"$childName\"\n# )
				{
					$parents{$parentName} .= "\"$pfPoPoName\",,\"$childName\"\n";
				}

				my $child = $parent->child('flow_ref');
				begat( $child, $compLib );
			}
			else
			{
				my $endData = ",,End Flow";
				if ( $parents{$parentName} !~ m#\"$pfPoPoName\"$endData$# )
				{
					$parents{$parentName} .= "\"$pfPoPoName\",,End Flow\n";
				}
			}
		}
		else
		{
			my $pfPoName = $pfPo->attribute('name');

			if ( $parent->child('flow_ref') )
			{
				my $child = $parent->child('flow_ref');
				my $childName = $parent->child('flow_ref')->attribute('name');

				if ( ! grep /$childName/, $parents{$parentName} )
				{
					$parents{$parentName} .= ",,\"$childName\"\n";
				}

				if ( ! grep /$pfPoName/, $parents{$parentName} )
				{
					getDetail( $pfPo, $parentName, ",,," );
				}

				begat( $child, $compLib );
			}
			else
			{
				my $endData = ",,End Flow";
				if ( ! grep /$endData/, $parents{$parentName} )
				{
					$parents{$parentName} .= "$endData\n";
				}

				if ( ! grep /$pfPoName/, $parents{$parentName} )
				{
					getDetail( $pfPo, $parentName, ",,," );
				}
			}
		}
	}

	my ( $parent, $compLib ) = @_;
	my $parentName = $parent->attribute('name');

	if ( ! exists( $parents{$parentName} ) )
	{
		push ( @parents, $parentName );
		$parents{$parentName} = "\"$parentName\",Next Action,Next Step\n";
	}

	if ( $parent->attribute('type') eq "goto" )
	{
		my @procFlow = $compLib->child('subroutine_flow')->child('flow')->child('process_flow');

		foreach my $procFlow (@procFlow)
		{
			if ( $procFlow->attribute('name') eq $parentName )
			{
				my $poName = $procFlow->child('process_object')->attribute('name');

				if ( ! grep /$poName/, $parents{$parentName} )
				{
					$parents{$parentName} .= "\"$poName\"";

					foreach my $xConn ($procFlow->child('process_object')->child('xconnector'))
					{
						my $xPoName = $xConn->child('process_object')->attribute('name');

						if ( $xConn->child('process_object')->child('process_object')->child('process_object') )
						{
							my $xPoPoPoName = $xConn->child('process_object')->child('process_object')->child('process_object')->attribute('name');
							$parents{$parentName} .= ",\"$xPoName\",\"$xPoPoPoName\"\n";
						}
						else
						{
							$parents{$parentName} .= ",\"$xPoName\",End Flow\n";
						}
					}
				}
			}
		}

		my @children = $parent->children('flow_ref');
       		foreach my $child ( @children )
		{
			begat( $child, $compLib );
		}
	}
	elsif ( $parent->attribute('type') eq "perform" )
	{
		if ( $parent->child( 'flow_ref' ) )
		{
			my $childName = $parent->child('flow_ref')->attribute('name');

			if ( ! grep /$childName/, $parents{$parentName} )
			{
				$parents{$parentName} .= ",,\"$childName\"\n";
			}

			my $child = $parent->child('flow_ref');
			begat( $child, $compLib );
		}
		else
		{
			my $endData = ",,End Flow";
			if ( ! grep /$endData/, $parents{$parentName} )
			{
				$parents{$parentName} .= "$endData\n";
			}
		}
	}
	elsif ( $parent->attribute('type') eq "complex" )
	{
		my @procFlow = $compLib->child('subroutine_flow')->child('flow')->child('process_flow');

		foreach my $procFlow (@procFlow)
		{
			if ( $procFlow->attribute('name') eq $parentName )
			{
				my $pfPo = $procFlow->child('process_object');
				getObject( $pfPo, $parent, $parentName, $compLib );
			}
		}
	}
  }
}
 
I can't see what the problem might be, sorry.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Are you sure you are getting any values that should be printed there.. ?? Uncomment out this line and see what you get to the screen

# print $parents{$parent} . "\n";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Sorry, didn't notice the [tt]begat()[/tt] call with (orror![evil]) variables in common with the calling block.
The problem comes exactly from this bad attitude: each time you call [tt]my @parents = ();[/tt] a new instance of this array is created, but the one used by [tt]begat[/tt] will stick to the first one created.

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
I moved the my @parents = (); and my %parents = (); outside of the foreach my $file and it worked.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top