CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



vid401t
New User

Nov 7, 2012, 4:53 AM

Post #1 of 6 (2008 views)
Converting one-liner to script Can't Post

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

Post #2 of 6 (2002 views)
Re: [vid401t] Converting one-liner to script [In reply to] Can't Post

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

Post #3 of 6 (1996 views)
Re: [BillKSmith] Converting one-liner to script [In reply to] Can't Post

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

Post #4 of 6 (1995 views)
Re: [vid401t] Converting one-liner to script [In reply to] Can't Post

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

Post #5 of 6 (1993 views)
Re: [vid401t] Converting one-liner to script [In reply to] Can't Post

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

Post #6 of 6 (1990 views)
Re: [BillKSmith] Converting one-liner to script [In reply to] Can't Post

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!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives