in reply to Bring two scripts together?

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.
#!/usr/bin/perl use strict; use warnings; # $IN = "someFileName.txt"; my $OUT = "someOutFile.xml"; open (my $fh_out, '>', $OUT) or die "can't open $OUT to write: $!"; #open(IN) or die ("Cannot open file ($IN)"); print $fh_out <<TOP; <?xml version="1.0" encoding="ISO-8859-1"?> <xtags:some-node xml:lang="en"> TOP my @columns = qw{ category id code title summary prereq group subgroup sequence rolemandatory rolerecommended roleoptional url modality length }; my $id; foreach my $row (<DATA>){ chomp $row; print $fh_out "\t<star:course>\n"; my %rec; @rec{@columns} = split ('\|', $row); for my $colname (@columns){ printf $fh_out "\t\t<xtag:%s>%s</xtag:%s>\n", $colname, $rec{$colname}, $colname; } print $fh_out "\t</xtag:course>\n"; $id++; } print $fh_out "</star:learning-paths>\n"; print "$id records\n"; __DATA__ a|a|a|a|a|a|a|a|a|a|a|a|a|a|a b|b|b|b|b|b|b|b|b|b|b|b|b|b|b c|c|c|c|c|c|c|c|c|c|c|c|c|c|c d|d|d|d|d|d|d|d|d|d|d|d|d|d|d e|e|e|e|e|e|e|e|e|e|e|e|e|e|e f|f|f|f|f|f|f|f|f|f|f|f|f|f|f g|g|g|g|g|g|g|g|g|g|g|g|g|g|g
update 1: added some output.
update 2: added chomp
update 3: added some tabs to the output
<?xml version="1.0" encoding="ISO-8859-1"?> <xtags:some-node xml:lang="en"> <star:course> <xtag:category>a</xtag:category> <xtag:id>a</xtag:id> <xtag:code>a</xtag:code> <xtag:title>a</xtag:title> <xtag:summary>a</xtag:summary> <xtag:prereq>a</xtag:prereq> <xtag:group>a</xtag:group> <xtag:subgroup>a</xtag:subgroup> <xtag:sequence>a</xtag:sequence> <xtag:rolemandatory>a</xtag:rolemandatory> <xtag:rolerecommended>a</xtag:rolerecommended> <xtag:roleoptional>a</xtag:roleoptional> <xtag:url>a</xtag:url> <xtag:modality>a</xtag:modality> <xtag:length>a</xtag:length> </xtag:course> <star:course> <xtag:category>b</xtag:category> <xtag:id>b</xtag:id> <xtag:code>b</xtag:code> <xtag:title>b</xtag:title> <xtag:summary>b</xtag:summary> <xtag:prereq>b</xtag:prereq> <xtag:group>b</xtag:group> <xtag:subgroup>b</xtag:subgroup> <xtag:sequence>b</xtag:sequence> <xtag:rolemandatory>b</xtag:rolemandatory> <xtag:rolerecommended>b</xtag:rolerecommended> <xtag:roleoptional>b</xtag:roleoptional> <xtag:url>b</xtag:url> <xtag:modality>b</xtag:modality> <xtag:length>b</xtag:length> </xtag:course>

Replies are listed 'Best First'.
Re^2: Bring two scripts together?
by lakeTrout (Scribe) on Jan 22, 2007 at 17:22 UTC
    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