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
gives a "Bareword not allowed" warning. The Perl quoted word function is useful in this case:my @columns = (category,id,code,title,summary);
And blow some white space in there while we're about it. :-)my @columns = qw{ category id code title summary prereq group subgroup sequence rolemandatory rolerecommended roleoptional url modality length };
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.
You can then build your record like this:@rec{@columns} = split ('\|', $row);
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.printf $fh_out "<xtag:%s>%s</xtag:%s>\n", $colname, $rec{$colname}, $colname;
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.update 1: added some output.#!/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
<?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>
In reply to Re: Bring two scripts together?
by wfsp
in thread Bring two scripts together?
by lakeTrout
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |