LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-22-2011, 11:29 AM   #1
Eppo
Member
 
Registered: Feb 2007
Location: NY
Distribution: Arch, Ubuntu
Posts: 77

Rep: Reputation: 27
setting new variable to regex of another without changing the other variable in perl


currently i have a Variable named $name which contains "JOHN,SMITH".
What i'm looking to do is take the first letter of the first and last name and assign it to another variable without changing $name.
what i tried was this:
Code:
$OF = $name =~ s/^(.).*,(.).*/$1$2/;
so what i'm getting is a value of 1 for $OF and name is changing to JS.
how do i get $OF to be JS and $name to stay "JOHN,SMITH"
thanks

Edit: figured it out:

Code:
($OF = $name) =~ s/^(.).*,(.).*/$1$2/;

Last edited by Eppo; 12-22-2011 at 11:56 AM. Reason: Solved
 
Old 12-22-2011, 02:35 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi Eppo,

it becomes more obvious what you want to achieve when you split the name at first and then use a regex for each word, here's the example:
Code:
#!/usr/bin/perl

use strict ;
use warnings ;

my $john = "John,Smith" ;

my @name = split ",", $john ;

my $name ;
foreach $name (@name) {
   $name =~ m/(^.)/ ;
   print $1 ;
}
Markus
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
using variable inside regex pauld Programming 10 09-15-2011 09:33 AM
awk regex with variable bertl1982 Linux - General 2 03-17-2010 08:38 AM
[SOLVED] How to include a string variable in a regex (Perl) MTK358 Programming 5 01-09-2010 11:31 AM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM
Changing encoding- Setting an environment variable.. rixride00 Programming 1 04-20-2004 03:25 AM

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

All times are GMT -5. The time now is 07:30 AM.

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