Sam Dela Cruz
1/10/2006 1:46:00 AM
I got different results in Perl and Ruby of this regular expression. Can
somebody maybe give me a "Ruby Way" solution of this? The output from
Perl is what I want. But I'm currently programming this is Ruby.
In Perl:
#snippet start ==========
sub transform_data
{
my $data = shift;
if ($data=~/^[\d\.]+$/) #numbers
{
print "Got here!\n";
}
else
{
print "'$data'","\n";
}
}
my $data = "patched 3 systems:
134.27.56.237
134.27.59.6
134.27.55.43";
transform_data($data);
#snippet end =========
Output is:
'patched 3 systems:
134.27.56.237
134.27.59.6
134.27.55.43'
---------------------------------------------------------------------------------------------------------------
In Ruby:
#snippet start ============
def transform_data(data)
if (data=~/^[\d\.]+$/) #numbers
puts "Got here!"
else
puts("'" + data + "'")
end
end
data = "patched 3 systems:
134.27.56.237
134.27.59.6
134.27.55.43"
transform_data(data)
#snippet end ============
Output is:
Got here!
Now why would it match in Ruby? Am I missing something here?
Regards,
Sam