CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Search and Copy to 2 files


New User

Mar 16, 2011, 1:23 AM

Post #1 of 7 (8730 views)
Search and Copy to 2 files Can't Post

In an xml-file I have an unknown number of rows. Each row have a unique name, in this case "app_name".
The rows also have a text that may be unique but = Music Player

<string name="app_name">Music Player</string>

I want to copy "app_name" into a file and it should look like this:

<TextName Name="app_name">

I also want to copy the text Music Player into another file like this:

<Text>Music Player</Text>

When this is done the next name in <String should be read and copied into the same files but the <EntryId> should be 2 and so on.


Mar 16, 2011, 9:15 AM

Post #2 of 7 (8724 views)
Re: [FrederickS] Search and Copy to 2 files [In reply to] Can't Post

This seems to do the job. Any problems with special cases?



perl intext outtext vartext

use strict;
use warnings;

my ($intext, $outtext, $vartext) = @ARGV;
open my $INTEXT, '<', $intext or die "Cannot open $intext\n";
open my $TEXT, '>', $outtext or die "Cannot open $outtext\n";
open my $VAR, '>', $vartext or die "Cannot open $vartext\n";
my $entryid=0;
while (my $line = <$INTEXT>) {
my ($app_name, $text_name)
= $line =~ /<string name="(\w+)">([\w ]+)<\/string>/;
next if !defined $app_name and !defined $text_name;
print {$TEXT}
qq(<TextName Name="$app_name">\n)
.qq(<EntryId>) . $entryid . qq(</EntryId>\n)

print {$VAR}
close $INTEXT;
close $TEXT;
close $VAR;

Good Luck,

New User

Mar 17, 2011, 12:27 AM

Post #3 of 7 (8707 views)
Re: [BillKSmith] Search and Copy to 2 files [In reply to] Can't Post

Thank you very much for the script :). Unfortunatly it's not working for me because I'm working in Windows with Strawberry Perl and I can not open the text files.
I have the files intext.xml, outtext.xml and vartext.xml. Can you help me with the changes I need to do to find/open the files?


Mar 17, 2011, 9:25 AM

Post #4 of 7 (8698 views)
Re: [FrederickS] Search and Copy to 2 files [In reply to] Can't Post

I am using Activestate 5.8.8 under Windows XP. I doubt that Strawberry or windows version make any difference. Your problem is more likely with windows than with perl. If all three files are in the current directory, and you type the command in a command (DOS) window, it should work.

If you cannot use the current directory, specify the full path of each file. Long directory or flle names are not a problem in XP or newer, but paths which contain spaces must be enclosed in double quotes.

If you get a message "Cannot open.....", Change the \n to $! at the end of the open statements and try again. This should print a windows error message that should explain the problem.

Double-clicking on the icon for the perl file will not work, there is no way to specify the files.

You can create a shotcut to the perl file. The target field shoud be the same as a command line. Specify the full path to each file. Use the quotes if needed.
Good Luck,

New User

Mar 18, 2011, 3:45 AM

Post #5 of 7 (8679 views)
Re: [BillKSmith] Search and Copy to 2 files [In reply to] Can't Post

This is the error I get: "Use of uninitialized value $intext in open at...."
I have tried to specify the full path but I'm not sure I'm doing it right, maybe you could give en example of how to do it?


Mar 19, 2011, 10:51 AM

Post #6 of 7 (8632 views)
Re: [FrederickS] Search and Copy to 2 files [In reply to] Can't Post

Assume that your three data files are called intext.txt, outtext.txt and vartext.txt

and are stored in the "My Documents" folder and that the perl script is called

and is stored in your home directory.

I will also assume that your windows login name is FredrickS and that the perl.exe is in your path.

By default, in XP, the path to "My Documents" is

C:\Documents and Settings\FredrickS\My Documents

and the Command Prompt accessory opens in the home directroy.

In this case, the path to the home directory does not make any difference.

Open the command prompt. From your start button, choose:

start\All Programs\Accessories\Command prompt.

Verify that your script is present.

At the command prompt, type: dir

Verify that your input data is present.

Type: dir "C:\ Documents and Settings\FredrickS\My Documents\intext.txt"

The Quotes are required because of the spaces in the directory names.

Verify that perl is available. Type: perl -v

If you have found both files and perl, you are ready to start.

Type the following command as if it were all on one line.

It will wrap to the next line. Just keep typing, let windows take care of displaying it. Leave a single space petween the path names.



"C:\Documents and Settings\FredrickS\My Documents\intext.txt"

"C:\Documents and Settings\FredrickS\My Documents\outtext.txt"

"C:\Documents and Settings\FredrickS\My Documents\Vartext.txt"

Note: This command has nothing to do with perl.

It is the DOS command to run your perl script.

After you have it working, we can consider options to reduce the typing.
Good Luck,

New User

Mar 20, 2011, 11:26 PM

Post #7 of 7 (8521 views)
Re: [BillKSmith] Search and Copy to 2 files [In reply to] Can't Post

Thank you. Everything is working now.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives