Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Perl help

#1
I want to rename some files from foo.jpg.1 to foo.jpg. They all have a numeric extension and can be jpeg, jpe or jpg.

Why doesn't this (air) code work?

Code:
#!/usr/bin/perl
# use strict;
# use warnings;

my $name = $_;
my $file_ext_original = ($name =~ m/([^.]+)$/)[0];

if($file_ext_original eq ["1", "2", "3", "4", "5", "6", "7", "8", "9"]) {
    print "$name needs to be renamed\n";
} else {
    print "$name is good\n";
}

Is it possible to use this string comparison or something like it?
Code:
$file_ext_original eq ["1", "2", "3", "4", "5", "6", "7", "8", "9"]

Instead I used a simple switch (again some air code)
Code:
use 5.010;

given ($file_ext_original) {
    when (["1", "2", "3", "4", "5", "6", "7", "8", "9"]) {
        print "$name needs to be renamed\n";
    }
    default {
#        print "$name is good\n";
    }
}
Reply

#2
Why not use
Code:
$file_ext_original ~= /[0-9]/

What you're doing with ["1", "2", ... , "9"] is create an array ref pointing to an array cointaining 1, ..., 9. You're comparing an array ref to a scalar. Even if you provided an array instead of an array ref by using parentheses instead of brackets, the array would be interpreted in scalar context, which means you would be comparing your lhs with the array's length, i.e. 9. To do exactly what you seem to be wanting to do, you need to use grep, or the smart matching operator ~~ which basically works as a shorthand for grep in that context. EDIT: but don't use an array ref, use an actual array.

EDIT: oops, smart matching implicitly dereferences array refs, so it works just fine with array refs. And given/when uses smart matching, so that explains why your array ref comparison is "smart" and works.
Reply

#3
Cheers MrBougo Angel Angel

I was hoping to create an array and then use a simple comparison to check if any of the array elements equalled $file_ext_original.
Code:
$file_ext = "mp3";
@array = qw(mp3 png jpg jpeg par2);

if ($file_ext == @array) {
    print "$file_ext is wanted\n";
} else {
    print "$file_ext: unknow\n";
}

A simple foreach works, but...
Code:
my $element = "";
my $file_ext = "mp3";
my @array = qw(png jpg mp3 jpeg par2);

foreach $element (@array) {
    if ($file_ext eq $element) {
        print "$file_ext is wanted\n";
    } else {
        print "$file_ext: unknow\n";
    }
}

# only using numbers
$file_ext = "7";
@array = (1..10);

foreach $element (@array) {
    if ($file_ext eq $element) { # eq or ==
        print "$file_ext is wanted\n";
    } else {
        print "$file_ext: unknow\n";
    }
}
its ugly.

Unfortunately Perl regex's are very greedy and confused.
Code:
@array = qw(mp3 m3p 3mp);
push(@array, (1..10));

foreach $element (@array) {
    if ($element =~ /[0-9]/) {
        print "a: $element has a number\n";
    }
}
print "\n\n";
foreach $element (@array) {
    if ($element =~ /^[0-9]+$/) {
        print "b: $element is +ve integer\n";
    }
}

Using smart matching
Code:
my @array = (1..20);
foreach my $element (@array) {
    if($element ~~ ["1", "2", "3", "4", "5", "8", "9", "10"]) {
        print "$element needs to be renamed\n";
    } else {
        print "$element is good\n";
    }
}
Reply

#4
Smart matching does not need array refs, and you could simply use (1..10) in the rhs.

For the regex one, why don't you match the whole file name against /\.(.+?)\.[0-9]+$/ and then grep for $1 in @array? +? means non-greedy +, it'll take the shortest match it finds.

EDIT: actually, why not just rename $file to the match of $file =~ s/(.*\.(?:mp3|jpg|blah|bleh)\.[0-9]+/ ?
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Help me choose new domain name Ch4mp 23 17,534 07-01-2016, 10:17 AM
Last Post: Ch4mp
  Need help identifying a part Lee_Stricklin 9 11,746 04-13-2015, 06:00 PM
Last Post: Lee_Stricklin
  Help liberate ROTC:Ethernet poVoq 8 7,824 02-01-2014, 01:00 PM
Last Post: poVoq
Question Help finding a song The mysterious Mr. 4m 1 3,536 10-10-2013, 12:28 PM
Last Post: Mirio
  I think I screwed up my GTK/GNOME/GWhatever, help? Minkovsky 5 6,138 08-12-2012, 10:30 AM
Last Post: Minkovsky
  A *Mythos Story (that you help to write) Minkovsky 7 8,465 04-14-2012, 07:22 AM
Last Post: Minkovsky
  Help fight internet censorship and SOPA! MirceaKitsune 22 26,230 03-08-2012, 03:24 PM
Last Post: Maddin
  Need help with music producing! kllleE 9 11,192 12-24-2011, 08:50 AM
Last Post: bitbomb
  Help support a new indie project! Samual 2 11,713 12-11-2011, 08:38 AM
Last Post: unfa
  Iron Fist Help? tempris 6 6,114 06-29-2011, 03:44 AM
Last Post: CuBe0wL

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB original theme © iAndrew 2016, remixed by -z-