http://www.perlmonks.org?node_id=994338

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

I need to subtract one digit from the last octet of the first ip and i need to add one to the last octet of the second ip address.

I am obviously struggling with this as my current code will dictate.

SAMPLE STRING: 192.168.1.1-192.168.1.254 needs to be 192.168.1.0-192.168.1.255

I have many strings of text that are random but the format is similar but the sample string can be 172.15.29.21-172.15.30.23 needs to be 172.15.29.20-172.15.30.24. These are all made up. In the first octet there will never be a zero as the last digit so only the very last digit in the octet needs to be regexed and subracted.

#!C:/Perl/bin/perl.exe use strict; use warnings; my $ip_1 =~ m/(\d)-/g; $ip_1 = int($ip_1); my $ip_2 = $ip_1 - 1; my $input_file = 'CrystalReportViewer1.csv'; my $output_file = 'CrystalReportViewer2.csv'; open(my $output_fh, '>', $output_file) or die "Failed to open $output_file - $!"; open(my $input_fh, '<', $input_file) or die "Failed to open $input_file: $!"; while ( <$input_fh> ) { s/$ip_1/$ip_2/; print {$output_fh} $_; } close $output_fh;