decrementation of a string in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting decrementation of a string in perl
# 1  
Old 08-09-2010
decrementation of a string in perl

Hi everybody.

I'm looking for a small function in perl to decrement a string.

For incrementing I could use the following script that is working without problem:

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $string=MNO;
my $n;
print "\n start value : string\n";

for ($n=1; $n<=3; $n++){
  print "step $n:\t$string\n";
  $string++;
}

When I change the line " $string++; " to " $string--; " the shell is giving me the following result:

Code:
start value : string
step 1: MNO
step 2: -1
step 3: -2


Last edited by pludi; 08-10-2010 at 02:26 AM.. Reason: code tags, please...
# 2  
Old 08-09-2010
The auto-increment operator (++) has a little extra builtin magic to it. If the variable $string has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern /^[a-zA-Z]*[0-9]*\z/ , the increment is done as a string, preserving each character within its range, with carry.

However, as you have found out, the auto-decrement operator (--) is not magical.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove not only the duplicate string but also the keyword of the string in Perl

Hi Perl users, I have another problem with text processing in Perl. I have a file below: Linux Unix Linux Windows SUN MACOS SUN SUN HP-AUX I want the result below: Unix Windows SUN MACOS HP-AUX so the duplicate string will be removed and also the keyword of the string on... (2 Replies)
Discussion started by: askari
2 Replies

2. Shell Programming and Scripting

Perl Look for string in file

Hi Guys In perl how can i look for a string in side a file and return something if it exists a little bit of background i have made a wrapepr for a program but in order for the program to work i need to modify a file first, i want to stick something in the wrapper that will tell if the sting... (1 Reply)
Discussion started by: ab52
1 Replies

3. Programming

PERL \c char in the string

Hi guys, I am stuck up in a situation. I have a SUN box with certain logs which I need to parse to draw a report using Perl. Now, when I load the text file using a perl degugger to see how the text looks like when the first line of the log file is read in a variable. below is the snapshot of... (2 Replies)
Discussion started by: Asteroid
2 Replies

4. Shell Programming and Scripting

How to replace string in perl?

Hi, I have string like this: $query="#1,apple"; $string=$query; I want to replace #1 with fruit. I tried like this: string=~s/#\d+/$query/ig; print "\n string: $string\n"; It is working only when there is single #1 or #2 but when i give like #1,#2,#3,apple the above code... (2 Replies)
Discussion started by: vanitham
2 Replies

5. Shell Programming and Scripting

perl get partial string of a string

Hi All, I have: $d = "12.02222222222"; $d =~ s/(.*).(.*)/$1/e; The output should be just 12. Please guide me my expression wrong. Thanks (5 Replies)
Discussion started by: jimmy_y
5 Replies

6. Shell Programming and Scripting

[Perl] Find one string, change another string.

Hi, In principle I am searching for a Perl equivalent for this sed command: sed "/TIM_AM_ARGS=/ s/60/1440/" $EDIT_FILE > $TEMP_FILE cp $TEMP_FILE $EDIT_FILE I was wondering if it needs to be like this, or that there other, shorter, alternatives: open (TIMENVFILE, "<$timenvfile") or die... (5 Replies)
Discussion started by: ejdv
5 Replies

7. Shell Programming and Scripting

string substitution in perl

Hi, I have a template file and want to replace 3 parameters to the values that I want. these values are in a parameter file. Any idea how to do this in perl? the parameter file looks like: host_name = jupiter PORT = 1562 IPADDRESS = 10.1.34.10 the template file has lots of entry.... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

8. Shell Programming and Scripting

Perl: Better way to match string within a string

Hi, I'm trying to get one field out of many as follows: A string of multiple fields separated with "/" characters: "/ab=12/cd=34/12=ab/34=cd/ef=pick-this.one/gh=blah/ij=something/" I want to pick up the field "ef=pick-this.one" which has no regular pattern except it starts with "ef=xxxx"... (3 Replies)
Discussion started by: Juha
3 Replies

9. Shell Programming and Scripting

Find String in Perl

Hi all, I am trying to search a SQL string and trying to find the table name in the query. For e.g. Select a,b,c,d,e,f from ITS.table where ITS.x=name In the above string i need to identify the word ITS and replace with owner. And also the name is not always ITS, it can be any... (11 Replies)
Discussion started by: mahalakshmi
11 Replies

10. Shell Programming and Scripting

String Replacement with Perl

I want to replace a string within a file using perl. We have a line that gets commented out, and I want to replace that line now matter how it was commented out. for example, I'd want to replace ###ES=PR1A with ES=PR1A or ##LJW(9/16/26)ES=PR1A with ES=PR1A I tried: perl... (4 Replies)
Discussion started by: Lindarella
4 Replies
Login or Register to Ask a Question