LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-05-2012, 01:04 AM   #1
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Rep: Reputation: 15
perl: fail to substitute negative value


I am using perl script to do some replacement as below:

@file[$line] =~ s/$sub_var_list=\d+/$sub_var_list=$sub_var_list_val/ ;

where #sub_var_list=noise6 in this case

i fail to substitute if noise6=-1, it is due to the negative sign. It is ok if the noise6=1 which is the positive value

part of the content file as below:
+ noise4=1 noise5=1 noise6=-1

pls help

Last edited by casualzone; 06-05-2012 at 01:06 AM.
 
Old 06-05-2012, 01:11 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
If both sides of the regex have the same string $sub_var_list, why would you use a variable?

Code:
$file[$line] =~ s/([A-Za-z0-9])=\d+/$1=$sub_var_list_val/ ;
(not tested).

Also you should probably check if you really mean @file[$line] instead of $file[$line]!

Markus

Last edited by markush; 06-05-2012 at 01:19 AM.
 
Old 06-05-2012, 02:05 AM   #3
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
may i know why changing from @file to $file?
I load the whole file into a memory array @file then read line by line of the file.
 
Old 06-05-2012, 02:19 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
@file is an array, an element of an array is $file[$index]. This is the usual way, see for example here: http://www.tizag.com/perlT/perlarrays.php

the other problem is the - sign, it is missing from your regexp:
Code:
$file[$line] =~ s/$sub_var_list=-?\d+/$sub_var_list=$sub_var_list_val/ ;

Last edited by pan64; 06-05-2012 at 02:27 AM.
 
1 members found this post helpful.
Old 06-05-2012, 03:06 AM   #5
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
oh ic
using "-?"

thank you very much
 
Old 06-05-2012, 03:34 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, \d means a digit, + means at least one (or more), - means the minus sign, ? means zero or one instance (of the previous something)
see regexp quantifiers.






_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
Old 06-05-2012, 07:06 AM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Ahh, I did not really understand what the OP means with "-1"....

I would suggest an additional solution
Code:
$file[$line] =~ s/$sub_var_list=[-+]?\d+/$sub_var_list=$sub_var_list_val/ ;
which also is valid in cases where the number comes with a +sign.

And an additional explanation about the difference between @ and $. Perl is very contextsensitive. In your case the substitution is done for a line which is a scalar (a single string) and therefore it is $file[$line].
If you expect to make the substitution for every line in @file you could instead write
Code:
s/$sub_var_list=[-+]?\d+/$sub_var_list=$sub_var_list_val/ for @file ;
which is a loop in a oneliner.

Markus
 
Old 06-06-2012, 12:14 AM   #8
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
ok
thanks for your sharing
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fail to connect mysql using perl!!? Shahid nx Linux - Newbie 3 02-09-2012 04:28 AM
[SOLVED] How substitute in a file when sed and awk both fail porphyry5 Linux - General 2 06-05-2011 01:41 PM
yum install perl Net::SSh fail powah Linux - General 1 09-11-2007 11:44 AM
perl -MCPAN -e 'install MIME::Base64' fail singying304 Linux - Software 7 11-25-2005 07:50 PM
Perl script: how to substitute through entire document? mister_math Programming 2 03-07-2003 11:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration