Home: Perl Programming Help: Beginner:
Converting one-liner to script



vid401t
New User

Nov 7, 2012, 4:53 AM


Views: 3829
Converting one-liner to script

Hi Everyone,

I'm trying to convert the following one liner into a perl script:
perl -ne 'chomp; print join(".", splice(@{[split/\./,$_]}, 0, -1), map {++$_} pop @{[split/\./,$_]}), "\n";'

What it does:


Code
$ cat versions 
1.2.3.44
1.2.3.9
1.2.3
9



Code
$ cat versions | perl -ne 'chomp; print join(".", splice(@{[split/\./,$_]}, 0, -1), map {++$_} pop @{[split/\./,$_]}), "\n";' 
1.2.3.45
1.2.3.10
1.2.4
10


I've tried using perl -MO=Deparse and inputting the code as follows into my script:


Code
$test = "1.2.4"; 

chomp $test;
print join('.', splice(@{[split(/\./, $test, 0)];}, 0, -1), map({++$test;} pop @{[split(/\./, $test, 0)];})), "\n";


But it keeps outputting: 1.2.2.2 <-- incorrect, should be 1.2.5

Any ideas or help?
Much appreciated in advance.


BillKSmith
Veteran

Nov 7, 2012, 6:59 AM


Views: 3823
Re: [vid401t] Converting one-liner to script

The easy way to convert you one-liner to a script is to put it in a file. Add the #! line to specify the path and flags.

Code
#!/usr/bin/perl -n 
chomp;
print join( ".",
splice( @{ [ split /\./, $_ ] }, 0, -1 ),
map { ++$_ } pop @{ [ split /\./, $_ ] } ),
"\n"
;


A less error prone way to increment version numbers is:

Code
#!/usr/bin/perl -n 
use strict;
use warnings;
chomp;
my @array = split /\./;
$array[-1]++;
join '.', @array;
local $" = '.';
print "@array\n";


In either case you could run them much like the one-liner.

Code
cat versions | perl script.pl 

or

perl script.pl < versions

Good Luck,
Bill


vid401t
New User

Nov 7, 2012, 10:39 AM


Views: 3817
Re: [BillKSmith] Converting one-liner to script

Hey Bill,

Thanks for the reply, much appreciated.

Last question, what if I was trying to use a string within the perl file to increment, how would that look?

Example:


Code
#!/usr/bin/perl -n  
use strict;
use warnings;

my $testString = "1.2.3";

# Below is not working for me as I get no output
my @array = split(/\./, $testString);
$array[-1]++;
local $" = '.';
print "@array\n";


Thanks again!

In Reply To


Laurent_R
Veteran / Moderator

Nov 7, 2012, 10:41 AM


Views: 3816
Re: [vid401t] Converting one-liner to script

BTW, you don't need this convoluted syntax:


Code
cat versions | perl ...


but you can simply run the one-liner on the file:




Code
perl -ne 'chomp; print join(".", splice(@{[split/\./,$_]}, 0, -1), map {++$_} pop @{[split/\./,$_]}), "\n";' versions



BillKSmith
Veteran

Nov 7, 2012, 11:16 AM


Views: 3814
Re: [vid401t] Converting one-liner to script

Remove the -n from the #! line.
Good Luck,
Bill

(This post was edited by BillKSmith on Nov 7, 2012, 12:02 PM)


vid401t
New User

Nov 7, 2012, 11:48 AM


Views: 3811
Re: [BillKSmith] Converting one-liner to script

Awesome!! thank you guys, worked out well, very helpful community btw, hope to get better at this and eventually help others out as well.

thanks again!