Perl Regex / String / Split

I need to be able to take a string and split it into two values.  Basically, I need two values from the string, however,  I need to "weed out" the stuff I don't need and JUST get the values needed.  Here's what the string looks like:

Image Width: 1419 Image Length: 1001

See code snippet for my current set of code.

Right now the output is showing as follows:

# ./test3.pl
First:
Second: 1419

What I need the output to be
# ./test3.pl
First: 1419
Second: 1001

So, I actually need the value of "Image Width" to be the first array value, and the value of "Image Length" to be the second array value.  

Thanks in advanced.
#!/usr/bin/perl
$r = "Image Width: 1419 Image Length: 1001";

@r = split(/: /,$r);
$r[0] =~ s/\D//g;
$r[1] =~ s/\D//g;

print "First: ".$r[0];
print "\n";
print "Second: ".$r[1];
print "\n";

Open in new window

LVL 2
drewrockshardAsked:
Who is Participating?
 
Terry WoodsConnect With a Mentor IT GuruCommented:

#!/usr/bin/perl
$r = "Image Width: 1419 Image Length: 1001";

@r = split(/(?<=\d)\D/,$r);
$r[0] =~ s/\D//g;
$r[1] =~ s/\D//g;

print "First: ".$r[0];
print "\n";
print "Second: ".$r[1];
print "\n";

Open in new window

0
 
zlobchoCommented:

#!/usr/bin/perl
$r = "Image Width: 1419 Image Length: 1001";
$r=~/.*:\s*(\w*)\s*.*:\s*(\w*)/;
print "First: $1\nSecond: $2\n";

Open in new window

0
 
TintinCommented:

#!/usr/bin/perl

$r = "Image Width: 1419 Image Length: 1001";
my ($width,$length) = $r =~ /:\s*(\d+).*:\s*(\d+)/;
print "first $width\n";
print "second $length\n";

Open in new window

0
 
FerrostiCommented:
Just for explanation...
Your split() takes everything until the first occurence of your delimiter ": ", content "Image Width". Since it does not contain any decimals $r[0] is empty at line 5.
0
 
drewrockshardAuthor Commented:
Awesome; this worked great!  
0
All Courses

From novice to tech pro — start learning today.