Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: Unable to f...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 10994 of 11509
Post > Topic >>

Re: Unable to finally commit the changes made to some lines in a

by krahnj@[EMAIL PROTECTED] (John W. Krahn) Apr 27, 2008 at 07:39 AM

diwsonal@[EMAIL PROTECTED]
 wrote:
> Hi,

Hello,

> I am trying to modify my iTunes Playlist and I want to prepend every
> playlist name with a specific pseudonym ...
> 
> I wrote a perl script using Perl::Tie module. Unfortunately, it doesnt
> write the contents of my array to the file after it is done processing
> each and every line in the file ...
> 
> I am attaching the script and the playlist file ...
> 
> Any hint how to finally untie and dump the modified contents to the
> same input file i.e. the playlist file ...
> 
> Thanks in advance..
> 
> --Sonal
> 
> Please find the script here:
> 
> #!/usr/bin/perl -w
> 
> use strict;
> use warnings;
> use Tie::File;
> 
> my $ITUNES_FILE_PATH = "/cygdrive/d/MyDo***ents/My Music/iTunes";
> my $ITUNES_LIBRARY_FILE_NAME = "iTunes Music Library1.txt";
> my $ITUNES_LIBRARY_FILE = "$ITUNES_FILE_PATH/
> $ITUNES_LIBRARY_FILE_NAME";
> my $PREPEND_PLAYLIST_KEY = "OEL: ";
> print "$ITUNES_LIBRARY_FILE\n";
> 
> tie my @[EMAIL PROTECTED]
 'Tie::File', $ITUNES_LIBRARY_FILE_NAME or die "Can't
> tie $ITUNES_LIBRARY_FILE $!";

When you use Tie::File any changes made to the contents of @[EMAIL PROTECTED]
 will 
be changed in the file itself.


> for my $i (0..$#lines) {
>    if ( $lines[$i] =~ /::/ ) {
> 	  my $matched_line = $lines[$i];

$matched_line is a copy of $lines[$i].

> 	  #print "$matched_line\n";
> 	  my @[EMAIL PROTECTED]
 = split(//, $matched_line);

@[EMAIL PROTECTED]
 is another copy of $lines[$i].

> 	  # split with null pattern will split on everything.
> 	  # so in this case it will be an array of characters
>       #print "$#charArray\n";
>       #print "$charArray[26]\n";
>       #print "$charArray[$#charArray-10]$charArray[$#charArray-11]\n";
> 
>       #Use this splice example to insert
>       #my @[EMAIL PROTECTED]
 = ("NS", "IE", "Opera");
>       #splice @[EMAIL PROTECTED]
 0, 0, 'Mosaic';
>       #print "@[EMAIL PROTECTED]
";
> 
>       splice(@[EMAIL PROTECTED]
 $#charArray-11, 2);
>       splice @[EMAIL PROTECTED]
 26, 0, $PREPEND_PLAYLIST_KEY;
>       print "@[EMAIL PROTECTED]
";

Nowhere do you modify $lines[$i] which means that you are not modifying 
the file itself.

>    }
> }
> untie @[EMAIL PROTECTED]
> #close $ITUNES_LIBRARY_FILE;
> 
> 
> Please find the file I am trying to modify here:
> 
> 	</dict>
> 			<key>Name</key><string>Sushma Darshan 08::</string>
> 			<key>Playlist ID</key><integer>61636</integer>
> 			<key>Playlist Persistent ID</key><string>9C1DDB98AD8E3878</string>
> 			<key>All Items</key><true/>
> 			<key>Playlist Items</key>
> 			<array>
> 				<dict>
> 					<key>Track ID</key><integer>16874</integer>
> 				</array>
> 		</dict>
> 	</dict>

It looks like you may need an XML module to work on that file?



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall
 




 2 Posts in Topic:
Unable to finally commit the changes made to some lines in a fil
diwsonal@[EMAIL PROTECTED  2008-04-26 22:20:32 
Re: Unable to finally commit the changes made to some lines in a
krahnj@[EMAIL PROTECTED]   2008-04-27 07:39:12 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 23 20:57:03 CDT 2008.