LinuxQuestions.org
Help answer threads with 0 replies.
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 08-24-2016, 09:26 AM   #1
Gary Baker
Member
 
Registered: Mar 2007
Location: Whitsett,NC
Distribution: Slackware 14.1 and MINT 17.1
Posts: 105

Rep: Reputation: 3
Stuck on perl Regex problem need help?


#!/usr/bin/perl

while($line = <STDIN>) {
$line =~ s/^.*$/"\^$&"/;
print ($line);
}

I have the above. What I am trying to do is to add quotes to the original output. The above adds quotes but also adds the caret. Thanks in advance.
 
Old 08-24-2016, 12:02 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
#!/usr/bin/perl

while(<>) {
  s/.*/"$&"/;
  print;
}
 
2 members found this post helpful.
Old 08-24-2016, 12:25 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,897

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
why regexp?
Code:
$line="\"$line\"";
 
4 members found this post helpful.
Old 08-24-2016, 03:46 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Yes, could also use qq function to avoid escaped quotes
Code:
$line = qq/"$line"/;
 
1 members found this post helpful.
Old 08-24-2016, 05:24 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Quote:
Originally Posted by Gary Baker View Post
$line =~ s/^.*$/"\^$&"/;
}

I have the above. What I am trying to do is to add quotes to the original output. The above adds quotes but also adds the caret. Thanks in advance.
It appears to me that you are telling Perl that you specifically want(!) that caret character!

The first part of the s/from/to/ regex is ... well ... a pedantic way to say: "the entire string."

The second part then says to substitute:
  • A double-quote.
  • The character "^", which has been escaped with a backslash to be sure that Perl sees it as a literal character.
  • The Perl variable $&.
  • A double-quote.
"You asked for it, so you got it."
 
1 members found this post helpful.
Old 08-24-2016, 06:11 PM   #6
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
Do like @pan64 posted. Larry himself does not recommend regex. But the other point of this is to go with the simplest approach. Regex is great and perl really cemented into the minds of many but it's not often simple.
 
Old 08-24-2016, 06:16 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
"Meh ... who really cares what The Larry™ thinks?"

But, "yes, certainly ..." there was no actual reason to use regular expressions in this case.
 
Old 08-25-2016, 12:39 PM   #8
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs
But, "yes, certainly ..." there was no actual reason to use regular expressions in this case.
May be there was, only the OP knows certainly. But maybe someone posts a benchmark to show the results for a million goes of each.
 
Old 08-26-2016, 04:16 AM   #9
Gary Baker
Member
 
Registered: Mar 2007
Location: Whitsett,NC
Distribution: Slackware 14.1 and MINT 17.1
Posts: 105

Original Poster
Rep: Reputation: 3
Wink

Thanks guys I don't know why I use regex. The answers were great!
 
Old 08-26-2016, 10:00 AM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Quote:
Originally Posted by Rinndalir View Post
May be there was, only the OP knows certainly. But maybe someone posts a benchmark to show the results for a million goes of each.
"A million goes of" anything at all is basically going to be an exercise in brutalizing the memory-manager.

String manipulation, no matter how you do it, can be very brutal because it "fills the air with sawdust and wood chips." I once worked with a high-performance section of an application which purposely used C(!) to do certain string operations ... in fixed arrays. It accepted dynamic-string inputs, dutifully tested them for excessive length, then performed a variety of operations in the "old school" way, purposely avoiding the dynamic allocation of memory. The working-set size (amount of VM typically allocated to and needed by the process) was dramatically reduced, and performance enhanced on the machine as a whole ... which ran multiple copies of this system at the same time.
 
Old 08-26-2016, 10:52 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please remember to mark as SOLVED once you have your solution.
 
Old 08-26-2016, 11:08 AM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by sundialsvcs View Post
"A million goes of" anything at all is basically going to be an exercise in brutalizing the memory-manager.

String manipulation, no matter how you do it, can be very brutal because it "fills the air with sawdust and wood chips." I once worked with a high-performance section of an application which purposely used C(!) to do certain string operations ... in fixed arrays. It accepted dynamic-string inputs, dutifully tested them for excessive length, then performed a variety of operations in the "old school" way, purposely avoiding the dynamic allocation of memory. The working-set size (amount of VM typically allocated to and needed by the process) was dramatically reduced, and performance enhanced on the machine as a whole ... which ran multiple copies of this system at the same time.
:-)
That is the usual solution to C++ problems as well... as well as any language using dynamic strings.
 
Old 08-26-2016, 03:24 PM   #13
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
Code:
First Method:  3 wallclock secs ( 2.46 usr +  0.61 sys =  3.07 CPU) @ 291757.98/s (n=895697)
Second Method:  3 wallclock secs ( 0.79 usr +  2.47 sys =  3.26 CPU) @ 237464.11/s (n=774133)
Third Method:  3 wallclock secs ( 1.79 usr +  1.26 sys =  3.05 CPU) @ 73415.41/s (n=223917)
Underdog Method:  4 wallclock secs ( 2.68 usr +  0.42 sys =  3.10 CPU) @ 338334.84/s (n=1048838)
Will post the methods later.
 
  


Reply

Tags
perlscript



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
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 04:36 AM
[Perl] Regex problem Ziddy Programming 6 12-21-2012 01:37 PM
[SOLVED] Perl one liner - problem with printing captured regex on new line uncle-c Programming 2 01-18-2012 05:15 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM

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

All times are GMT -5. The time now is 06:04 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