Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Breaking out of a while loop: Is there a better way?

by BubbaMonk (Sexton)
on Nov 02, 2006 at 01:35 UTC ( [id://581805]=perlquestion: print w/replies, xml ) Need Help??

BubbaMonk has asked for the wisdom of the Perl Monks concerning the following question:

Whazzzup?

So, I searched, super searched, cracked open the Nutshell, and still can't find a better way to do this aweful thing (break out of the while loop once the script finds the drive letter it's looking for:
open PLAY, $playlist . ".wpl"; # Get original drive letter my $original_drive_letter; while (<PLAY>) { print "$counter\n"; if (/src=\"(.)\:\\/) { $original_drive_letter = $1; close PLAY; } }
Simple. Read the file line by line but stop once Perl finds the drive letter. But there's this pesky readline on closed filehandle warning. Any better way to do this?

So what's this code supposed to do, anyways?
My mp3's live on an external hardisk. Stupid windows doesn't always assign the same drive letter to the drive, so my playlists break, dag nabit. So, I came up with this:
#!perl use strict; use warnings; print "Which playlist do you need to update?\n"; chomp (my $playlist = <STDIN>); $playlist =~ s/(.*)\.wpl/$1/; open PLAY, $playlist . ".wpl"; my $original_drive_letter; my $counter=0; # get drive while (<PLAY>) { ++$counter; print "$counter\n"; if (/src=\"(.)\:\\/) { $original_drive_letter = $1; close PLAY; } } print "It appears the original drive letter is $original_drive_letter. +\n"; print "What would you like the new drive letter to be?\n"; chomp (my $new_drive_letter = <STDIN>); my $no_integrity = 1; while ($no_integrity) { if ($new_drive_letter =~ /^[a-zA-Z]?$/) { print "You want the new drive letter to be $new_drive_letter.\n"; $no_integrity = 0; } else { print "$new_drive_letter doesn't seem to be a valid drive letter. +Let's try again.\n"; print "What do you want the new drive letter to be?\n"; chomp ($new_drive_letter = <STDIN>); $no_integrity = 1; } } print "\nUpdating playlist."; open PLAY, $playlist . ".wpl"; open WRITE, ">temp.wpl"; while (<PLAY>) { s/(="$original_drive_letter:\\)/="$new_drive_letter:\\/g; print WRITE "$_"; print "."; } print "\n\nFinished updating playlist."; close PLAY; close WRITE; unlink ($playlist . ".wpl"); rename "temp.wpl", $playlist . ".wpl" or warn "Couldn't rename: $!";
Media player is too dense to figure things out on its own. Perl 5.9, you're my hero.
Thanks for your advice!

Pizerl is the Bizomb, baby!

Replies are listed 'Best First'.
Re: Breaking out of a while loop: Is there a better way?
by Joost (Canon) on Nov 02, 2006 at 01:43 UTC
Re: Breaking out of a while loop: Is there a better way?
by graff (Chancellor) on Nov 02, 2006 at 01:44 UTC
    Are you maybe forgetting (needing to learn) about "last" ?
    # get drive while (<PLAY>) { ++$counter; print "$counter\n"; if (/src=\"(.)\:\\/) { $original_drive_letter = $1; last; # exit the while loop now } } close PLAY;
Re: Breaking out of a while loop: Is there a better way?
by smokemachine (Hermit) on Nov 02, 2006 at 01:45 UTC
    why dont you use a last?
      Because I didn't know it was there! That other language used break so I was looking for break. Maybe someone should make something like the unix apropos for Perl 6. That would give them, what, about five years to do it before the release? :~)
Re: Breaking out of a while loop: Is there a better way?
by smokemachine (Hermit) on Nov 02, 2006 at 02:24 UTC
    Couldnt simplificate this with somethink like this? perl -i -pe 'BEGIN{die "Wrong type of file or dont exist" unless $ARGV[0] =~ /\.wpl$/ and -e $ARGV[0]; open $PLAY, $ARGV[0]; while(<$PLAY>){$drive=$1,last if /src=\"(.)\:\\/}print {STDOUT} "It appears the original drive letter is $drive.\nWhat would you like the new drive letter to be?\n";chomp($letter=<STDIN>) until$letter =~/^[a-zA-Z]$/; print {STDOUT} "You want change the $drive drive letter to $letter.\n"}s/="$drive:\\/="$letter:\\/g; print {STDOUT} "."; END{print {STDOUT} $/}' file.wpl
      Obfuscation is in the eye of the beholder, eh. Doesn't look to simplicated to me! But that might be because I've never used -i or -pe before.....
      Because he works on windows (drive letter anyone) and windows has a limit on command line length. Old was 128 byte new is 1024(?)! So your command line may be too long;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://581805]
Approved by graff
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found