Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converts data in perl

Status
Not open for further replies.

encore4

IS-IT--Management
Nov 16, 2006
4
SE
Hi I am at a beginner level and I am using linux.I have searched the net, but I can't figure out how to solve this!



I need a script that takes sourcecode as input and converts it from $long_variable to $shortvariable

How do yu do that?
 
please explain your question better.

- Kevin, perl coder unexceptional! [wiggle]
 
what I need is a working regular expression

import for example this file with your script and write to it

###before change

#!/usr/bin/perl -w
use strict;
chdir('/home/johan/perlovn') or die "$!";
my $long_variabel_name;
while(<*>) {
$long_variabel_name = $_;
tr/A-Z/a-z/;
rename $long_variabel_name, $_;
}
###after change

#!/usr/bin/perl -w
use strict;
chdir('/home/johan/perlovn') or die "$!";
my $shortvariabelname;
while(<*>) {
$shortvariabelname = $_;
tr/A-Z/a-z/;
rename $shortvariabelname, $_;
}
 
seems easy enough, but this does not take into account some situations where you may not want to alter the scalar:

Code:
[olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red][red]{[/red]
   [red]s/[/red][purple]([purple][b]\$[/b][/purple]\w+)[/purple][red]/[/red][purple]makeshort([blue]$1[/blue])[/purple][red]/[/red][red]eg[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]makeshort[/maroon] [red]{[/red]
   [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$v[/blue] = [url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url][red];[/red]
   [blue]$v[/blue] =~ [red]tr/[/red][purple]_[/purple][red]/[/red][purple][/purple][red]/[/red][red]d[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url] [blue]$v[/blue][red];[/red]
[red]}[/red]	
[teal]__DATA__[/teal]
[teal]#!/usr/bin/perl -w[/teal]
[teal]use strict;[/teal]
[teal]chdir('/home/johan/perlovn') or die "$!";[/teal]
[teal]my $long_variabel_name;[/teal]
[teal]while(<*>) {[/teal]
[teal]   $long_variabel_name = $_;[/teal]
[teal]      tr/A-Z/a-z/;[/teal]
[teal]         rename $long_variabel_name, $_;[/teal]
[teal]         }[/teal]
[teal]###after change[/teal]

- Kevin, perl coder unexceptional! [wiggle]
 
Thanks
Sorry to be unclear;
I want the script to do exactly that you describe
"take into account some situations where you may not want to alter the scalar
 
For that you would have to check the context the scalar is being used in the perl code. That is much too complicated and beyond the scope of this forum and certainly beyond my ability to program such a script. But, the code I posted may work just like you want it too, so give it a try. Make sure to backup the file you run through it first though.

- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top