transfer data from one file to another
transfer data from one file to another
(OP)
I am trying to write a script which takes data from tab diliminted text file ,and stores it in an array and then i want to replace it in an xml file .
the code...
#!/usr/local/bin/perl
sysopen SOURCE,openfil,2 or die "can't open file SOURCE ";
$nfo = <SOURCE>;
close SOURCE;
print "$nfo";
open (TARGET,">>target") or die "Can't open the file TARGET ";
$tar = <TARGET>;
print TARGET "$nfo\n";
close TARGET;
print "$tar";
@it = split(/\t/,$nfo);
foreach $row (@it) {
print " this is each row $it :$row \n";
}
print "$it[0]";
I an open to all suggestions.
the code...
#!/usr/local/bin/perl
sysopen SOURCE,openfil,2 or die "can't open file SOURCE ";
$nfo = <SOURCE>;
close SOURCE;
print "$nfo";
open (TARGET,">>target") or die "Can't open the file TARGET ";
$tar = <TARGET>;
print TARGET "$nfo\n";
close TARGET;
print "$tar";
@it = split(/\t/,$nfo);
foreach $row (@it) {
print " this is each row $it :$row \n";
}
print "$it[0]";
I an open to all suggestions.
RE: transfer data from one file to another
$tar = <TARGET>;
this should probably be:
@nfo = <SOURCE>;
@tar = <TARGET>;
RE: transfer data from one file to another
#!/usr/local/bin/perl
sysopen SOURCE,openfil,2 or die "can't open file SOURCE ";
$_ = <SOURCE>;
close SOURCE;
print "$_";
@element = split (/\t/,$_);
print @element;
$data = target;
open (VARIABLE,'>>data.pl') or die "can't open the file VARIABLE";
$var = <VARIABLE>;
print "managed to open file";
close VARIABLE;
print '$var';
print "managed to open file";
if ($var = ~s/sourceid/element/gi) {
}
print "$var";
~
RE: transfer data from one file to another
open (VARIABLE,'>>data.pl') or die "can't open the file VARIABLE";
$var = <VARIABLE>;
you can't read from a file when you open it for appending.
you need to:
open (VARIABLE,'data.pl') or die "can't open the file VARIABLE";
then if you want all of the lines from the filehandle instead of just one, you need to :
@var = <VARIABLE>;
RE: transfer data from one file to another
i can't find the operator which you use to stop the search if you find the match.
RE: transfer data from one file to another
Ok, let's have a look at the code. First, I would suggest cleaning up a bit. If I understand correctly, you are trying to read a file containing a tab and comma delimited list and then do what? Well, here is code that should read your list, and then I will recreate the list again for you. This decomposition and recomposition should at least show you how to work with the file and the text.
#!/usr/local/bin/perl
# declare your file variables
my $source_file = "file1.name";
my $target_file = "file2.name";
# open the file
open(SOURCE,"$source_file") or die "$!";
# read the file into an array of lines
my (@SOURCE) = <SOURCE>;
# close your file
close(SOURCE);
# Join your array of lines into a single line.
# This assumes that there is nothing but a
# newline between them so it inserts a tab.
my $source = join("\t",@SOURCE);
# reuse your @SOURCE array to store the new
# array of comma delimited strings by
# splitting on the tabs
@SOURCE = split(/\t/,$source);
# now create sub-arrays to store your list
# of elements by splitting on the commas
for (my $x=0; $x<@SOURCE; $x++)
{
@SOURCE[$x] = split(/,/,$SOURCE[$x]);
}
# Ok, now we have everything all split up,
# so now we could do something with it.
# Here, I'll just make it into a comma delimited,
# tab delimited list again.
for (my $x=0; $x<@SOURCE; $x++)
{
$SOURCE[$x] = join(/,/,@SOURCE[$x]);
}
$source = join(/\t/,@SOURCE);
open(TARGET,">$target_file") or die "$!";
print TARGET $source;
close(TARGET);
I hope that is helpful.
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: transfer data from one file to another
#!/usr/local/bin/perl
#opening the file to read
sysopen SOURCE,openfil,2 or die "can't open file SOURCE ";
#saving the data in an array
@_ = <SOURCE>;
#check if content is in it
print @_;
#assigning value to x
$x=0;
#trying to store each line of the array in numerious arrays
foreach $line (@_) {
#storing each line in array element[0]...element[1] ,trying to s
plit the contents of the line in different elements of the array.
"\@element$x" = split (/\t/,$line); #i get an error when i try to r
eplace the value of x
print "\@element$x \n";
$x++;
}
close SOURCE;
RE: transfer data from one file to another
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: transfer data from one file to another
RE: transfer data from one file to another
Is there some part of your question I haven't answered with this illustration?
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: transfer data from one file to another
RE: transfer data from one file to another
the sample of the file that i am try to split is
Sourceid title id,id,id,id year yearid---use to create one xmlfile
Sourceid2 title2 ids,ids,ids year2 yearid2---use to create another xmlfile
the format of the data to be replaced in the file is
<Sourceid>sourceid</sourceid>
<title>title</title>
whatever is written in the text fdile is replaced in the xml file.
I am open to all suggestions about the logic of my code
thanks again for all the help.
RE: transfer data from one file to another
try
open ( MYFILE, "+>> file.txt" )
it works on my activeperl
RE: transfer data from one file to another
RE: transfer data from one file to another
#!/usr/local/bin/perl
# open the file
open(SOURCE,"source.file") or die "$!";
# read the file into an array of lines
my (@SOURCE) = <SOURCE>;
# close your file
close(SOURCE);
# This time we'll keep the array of lines since
# each line appears to be its own record
# create sub-arrays to store the
# array of elements in each record by
# splitting on the tabs
for (my $x=0; $x<@SOURCE; $x++)
{
@SOURCE[$x] = split(/\t/,$source);
}
# now create sub-arrays to store your list
# of ids by splitting on the commas
for (my $x=0; $x<@SOURCE; $x++)
{
for (my $y=0; $y<@SOURCE[$x]; $y++)
{
@SOURCE[$x][$y] = split(/,/,$SOURCE[$x][$y]);
}
}
# Ok, now we have everything all split up,
# so now let's write your xml files
for (my $x=0; $x<@SOURCE; $x++)
{
# open the target file
open(TARGET,">xml.file$x") or die "$!";
# print your first two elements
print TARGET qq~<sourceid>$SOURCE[$x][1]</sourceid>\n~;
print TARGET qq~<title>$SOURCE[$x][2]</title>\n~;
# print all of your id's
for (my $y=0; $y<@SOURCE[$x][3]; $y++)
{
print TARGET qq~<id$y>$SOURCE[$x][3][$y]</id$y>\n~;
}
# print the rest of your elements
print TARGET qq~<year>$SOURCE[$x][4]</year>\n~;
print TARGET qq~<yearid>$SOURCE[$x][5]</yearid>\n~;
# close the file
close(TARGET);
}
There, now you took your one source file and wrote a bunch of xml files based on that input.
Has that solved your problem?
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: transfer data from one file to another
{
@SOURCE[$x][$y] = split(/,/,$SOURCE[$x][$y]);
}
for (my $y=0; $y<@SOURCE[$x][3]; $y++)
{
print TARGET qq~<id$y>$SOURCE[$x][3][$y]</id$y>\n~;
}
Can't use subscript on array slice at mysource.pl line 28, near "$y]"
Can't use subscript on array slice at mysource.pl line 43, near "3]"
this generates an error,i have been trying to substitute in a for statement with arrays and it gives me the same error.
Thanks for all the help,i was wondering what qq~ does.
Thanks
Kavi98