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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



FrederickS
New User

Mar 16, 2011, 1:23 AM

Post #1 of 7 (4585 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">
<EntryId>1</EntryId>
</TextName>


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

<Start>
<EntryId>1</EntryId>
<Variant>
<Text>Music Player</Text>
</Variant>
</Start>

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.


BillKSmith
Veteran

Mar 16, 2011, 9:15 AM

Post #2 of 7 (4579 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?


Code
  

Usage:

perl splthtml.pl 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;
$entryid++;
print {$TEXT}
qq(<TextName Name="$app_name">\n)
.qq(<EntryId>) . $entryid . qq(</EntryId>\n)
.qq(</TextName>\n)
;

print {$VAR}
qq(<START>\n)
.qq(<EntryId>$entryid</EntryId>\n)
.qq(<Variant>\n)
.qq(<Text>$text_name</Text>\n)
.qq(</Variant>\n)
.qq(</START>\n)
;
}
close $INTEXT;
close $TEXT;
close $VAR;

Good Luck,
Bill


FrederickS
New User

Mar 17, 2011, 12:27 AM

Post #3 of 7 (4562 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?


BillKSmith
Veteran

Mar 17, 2011, 9:25 AM

Post #4 of 7 (4553 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,
Bill


FrederickS
New User

Mar 18, 2011, 3:45 AM

Post #5 of 7 (4534 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?


BillKSmith
Veteran

Mar 19, 2011, 10:51 AM

Post #6 of 7 (4487 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 splthtml.pl

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 splthtml.pl

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.


Code
  

perl splthtml.pl

"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,
Bill


FrederickS
New User

Mar 20, 2011, 11:26 PM

Post #7 of 7 (4376 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