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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

transfer data from one file to another 1

Status
Not open for further replies.

kavi98

Programmer
Dec 14, 2000
48
US
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 &quot;$nfo&quot;;

open (TARGET,&quot;>>target&quot;) or die &quot;Can't open the file TARGET &quot;;
$tar = <TARGET>;
print TARGET &quot;$nfo\n&quot;;
close TARGET;
print &quot;$tar&quot;;

@it = split(/\t/,$nfo);
foreach $row (@it) {
print &quot; this is each row $it :$row \n&quot;;
}
print &quot;$it[0]&quot;;

I an open to all suggestions.
 
$nfo = <SOURCE>;
$tar = <TARGET>;


this should probably be:

@nfo = <SOURCE>;
@tar = <TARGET>;
 
I am trying to replace a value in another file retreiving it from one file.it gives me a numeric value i have no idea why?

#!/usr/local/bin/perl

sysopen SOURCE,openfil,2 or die &quot;can't open file SOURCE &quot;;
$_ = <SOURCE>;
close SOURCE;
print &quot;$_&quot;;

@element = split (/\t/,$_);

print @element;
$data = target;
open (VARIABLE,'>>data.pl') or die &quot;can't open the file VARIABLE&quot;;
$var = <VARIABLE>;
print &quot;managed to open file&quot;;
close VARIABLE;
print '$var';
print &quot;managed to open file&quot;;

if ($var = ~s/sourceid/element/gi) {
}
print &quot;$var&quot;;

~
 

open (VARIABLE,'>>data.pl') or die &quot;can't open the file VARIABLE&quot;;
$var = <VARIABLE>;


you can't read from a file when you open it for appending.

you need to:

open (VARIABLE,'data.pl') or die &quot;can't open the file VARIABLE&quot;;
then if you want all of the lines from the filehandle instead of just one, you need to :

@var = <VARIABLE>;

 
I am try to read the substring which are going to be further split by comma's.
i can't find the operator which you use to stop the search if you find the match.
 
Yes, you can read a file opened for appending.

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.

Code:
#!/usr/local/bin/perl

# declare your file variables
my $source_file = &quot;file1.name&quot;;
my $target_file = &quot;file2.name&quot;;

# open the file
open(SOURCE,&quot;$source_file&quot;) or die &quot;$!&quot;;
# 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(&quot;\t&quot;,@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,&quot;>$target_file&quot;) or die &quot;$!&quot;;
print TARGET $source;
close(TARGET);

I hope that is helpful.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
I am trying to split one array into numerious arrays but when i try to name them with the loop generating numbers it generates an error.


#!/usr/local/bin/perl

#opening the file to read
sysopen SOURCE,openfil,2 or die &quot;can't open file SOURCE &quot;;
#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.

&quot;\@element$x&quot; = split (/\t/,$line); #i get an error when i try to r
eplace the value of x

print &quot;\@element$x \n&quot;;
$x++;
}
close SOURCE;
 
I just showed you how to do that. If you don't understand a part of the code, please specify which part is confusing you.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
i understand splitting it but if itry to name it then it generates an error,it would be helpful if you could look at the last part again .
 
No offense, but your code is sloppy... I'd rather not try to understand it. The code I wrote above works as you want it to. Every element in your list is in the @SOURCE array. $SOURCE[1][1] is the first element of your first comma-delimited list. $SOURCE[1][2] is the second element of your first comma-delimited list. $SOURCE[2][1] is the first element of your second comma-delimited list. Et cetera. If you want, you can do
Code:
my $first_list = @SOURCE[1];
, etc, to name the lists individually.

Is there some part of your question I haven't answered with this illustration?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
if you know a way to read a file by opening it for appending, i'd love to know what it is. because i can't seem to get it to work. maybe it's a bug in activeperl, but i doubt it.
 
Thanks for all the help you have given,AS i am a new programmer ,i still have a lot to learn.

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.
 
> if you know a way to read a file by opening it for appending

try

open ( MYFILE, &quot;+>> file.txt&quot; )

it works on my activeperl
 
Ok, kavi98, I'll rewrite my example to work on your specific problem:
[tt]
#!/usr/local/bin/perl

# open the file
open(SOURCE,&quot;source.file&quot;) or die &quot;$!&quot;;
# 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,&quot;>xml.file$x&quot;) or die &quot;$!&quot;;
# 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);
}
[/tt]
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.
 
for (my $y=0; $y<@SOURCE[$x]; $y++)
{
@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 &quot;$y]&quot;
Can't use subscript on array slice at mysource.pl line 43, near &quot;3]&quot;
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top