×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

transfer data from one file to another

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.

RE: transfer data from one file to another

$nfo = <SOURCE>;
$tar = <TARGET>;


this should probably be:

@nfo = <SOURCE>;
@tar = <TARGET>;

RE: transfer data from one file to another

(OP)
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 "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

(OP)
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.

RE: transfer data from one file to another

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.


#!/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

(OP)
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 "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

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.
http://www.oac-design.com

RE: transfer data from one file to another

(OP)
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 .

RE: transfer data from one file to another

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 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.
http://www.oac-design.com

RE: transfer data from one file to another

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.

RE: transfer data from one file to another

(OP)
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.

RE: transfer data from one file to another

> if you know a way to read a file by opening it for appending

try

open ( MYFILE, "+>> file.txt" )

it works on my activeperl

RE: transfer data from one file to another

oops, i forgot about that.

RE: transfer data from one file to another

Ok, kavi98, I'll rewrite my example to work on your specific problem:

#!/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

(OP)
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 "$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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close