Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Bring two scripts together?

by lakeTrout (Scribe)
on Jan 22, 2007 at 04:58 UTC ( [id://595862]=perlquestion: print w/replies, xml ) Need Help??

lakeTrout has asked for the wisdom of the Perl Monks concerning the following question:

Hey Folks,

I am working on a script using spreadsheet::parseExcel to eventually build out an XML file. A fellow PM helped me with the spreadsheet::parseEcel script and I've created the script to work out the XML. I know that using template would be have been a better option, but this "works" and is WAY overdue per time. Is there a whay to "merge" these two scripts in to one -- if so how? It can't be as simple as appending them? Getting this to one file is the critical goal here. Thank you all for your help, I've been running in circles!

UPDATE: Note that arg[1] is an excel spreadsheet (of course)
#!/usr/bin/perl -w use warnings; use strict; use Spreadsheet::ParseExcel; my $oExcel = new Spreadsheet::ParseExcel; my $dir = $ARGV[1]; chomp $dir; unless (-d $dir) { print "Can't find directory $dir"; exit; } my $filename = "$dir/$ARGV[0]"; chomp $filename; unless (-e $filename) { print "Can't find file $filename"; exit ; } my $fullfilename = "$filename.txt"; ## start work print "Converting..."; #1.1 Normal Excel97 open E2T, ">", $fullfilename or die $!; my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($filename); my($iR, $iC, $oWkS, $oWkC); foreach my $oWkS (@{$oBook->{Worksheet}}) { print "--------- SHEET:", $oWkS->{Name}, "\n"; print E2T $oWkS->{Name}, "|"; next unless defined $oWkS->{MinRow} and defined $oWkS->{MaxRow}; for my $iR ($oWkS->{MinRow} .. $oWkS->{MaxRow}) { print E2T "\n"; for my $iC ($oWkS->{MinCol} .. $oWkS->{MaxCol}) { $oWkC = $oWkS->{Cells}[$iR][$iC]; unless (!defined $oWkC){ print "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC); + print E2T $oWkC->Value; } print E2T "|"; } } print E2T "\n"; } close E2T; print "Finished!"; exit;
I also need to make sure that in my flatfile (pipe delimited) I replace empty || with |0| (that was explained here with great feedback.)

here is the "second" script:
#!/usr/bin/perl #this is the out from the previous script $IN = "someFileName.txt"; $OUT = "someOutFile.xml"; open (OUT, ">$OUT"); open(IN) or die ("Cannot open file ($IN)"); print OUT <<TOP; <?xml version="1.0" encoding="ISO-8859-1"?> <xtags:some-node xml:lang="en"> TOP $id = 0; @columns = (category,id,code,title,summary,prereq,group,subgroup,seque +nce,rolemandatory,rolerecommended,roleoptional,url,modality,"length") +; foreach $row (<IN>){ ($category,$code,$title,$summary,$prereq,$group,$subgroup,$sequence,$r +olemandatory,$rolerecommended,$roleoptional,$url,$modality,$length) = + split ('\|', $row); print OUT "<star:course>\n"; foreach $_ (@columns){ &xmlrow($_); } print OUT "</xtag:course>\n"; $id++; } print OUT "</star:learning-paths>\n"; sub xmlrow{ my $colname = shift(@_); my $ostar="<xtag:"; my $estar="</xtag:"; print OUT "<xtag:$colname>${$colname}</xtag:$colname>\n"; }

Replies are listed 'Best First'.
Re: Bring two scripts together?
by wfsp (Abbot) on Jan 22, 2007 at 10:00 UTC
    A few points about your second script.

    It's always a good idea to use strict and warnings like you have in your first script.

    It's also a good idea to use the three argument open, again, as you have in your first script. I also prefer to use a lexical file handle (see the docs).

    Under strict and warnings

    my @columns = (category,id,code,title,summary);
    gives a "Bareword not allowed" warning. The Perl quoted word function is useful in this case:
    my @columns = qw{ category id code title summary prereq group subgroup sequence rolemandatory rolerecommended roleoptional url modality length };
    And blow some white space in there while we're about it. :-)

    If you think about your column names as keys and the fields as values then it's worth considering a hash. This looks a bit odd but it's a hash slice which populates the hash keys from @columns using the fields as values.

    @rec{@columns} = split ('\|', $row);
    You can then build your record like this:
    printf $fh_out "<xtag:%s>%s</xtag:%s>\n", $colname, $rec{$colname}, $colname;
    Here we're using printf to make formating the string a bit easier to see. Because we have a hash we don't need your symbolic reference ${$colname} which is best avoided and won't work under strict and warnings anyhows.

    Other monks will likely suggest better ways to work with xml (it's not my forte) but if you are going to do it like this I hope my comments are of some use.

    Here's my crack at it.
      Wow, wfsp -- I greatly appreciate the feedback. I've been down all night but I'm diving in now to play with your solution/suggestions. What a great community, and ++ to you. I miss this place :) i'll try briniging these into a single file to run on one command. Thanks again!

      -lT
Re: Bring two scripts together?
by ahmad (Hermit) on Jan 22, 2007 at 06:55 UTC

    take your second script & put it before the exit in the first script then you'll get both scripts in one

    of course you'll need to change the $IN & $OUT in the second script to match what already used in the first one

    HTH

Re: Bring two scripts together?
by Sagacity (Monk) on Jan 22, 2007 at 15:07 UTC

    Hello lakeTrout,

    Take a look at these suggestions as well as the other posts!

    In my own adventures of programming I have run into some difficult debugging situations due to my mis-understanding of how Perl assigns and uses memory.

    I hope I can save you from problems you may encounter in the future if you stay the course of your current programming style.

    I have included your scripts with comments inside. I hope this can help you.

    The first file

    The second file

    UPDATE: There was an earlier suggestion that you add a require "second_file_name.pl" statement in order to append the second script. Simply place the require statement above the print "Finished\n"; statement in your first file. then, remove the #!/usr/bin/perl at the top of the second file.

    I placed a few more notes and suggestions in the readmores for you to look at. Hope this helps!!

    Remember: It's not a bug, It's a feature!! Credit must be given to the original Author of this tag line, but I can't remember who it is!

      Howdy!
      I really appreciate your suggestions and the amount of time you (and others) must have spent to help me. I have the two scripts running a little cleaner now, but can't seem to bring them together. Pardon is this is just an ignorant question of if I've just been looking at this too long. I appreciate the help or tips.

      -lT
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://595862]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found