CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
String manipulation: Fetching all occurances of string

 



dkhanna01
Novice

Feb 9, 2005, 4:06 AM

Post #1 of 6 (680 views)
String manipulation: Fetching all occurances of string Can't Post

I have string like this

$string1= "TECH01:-abcd,TECH02:-xyz"

Now I want to print only TECH01,TECH02 in the second string that is all the occurance of string stating with TECH. so that $string2 should become "TECH01,TECH02".

Please let me know how should I do this.


KevinR
Veteran


Feb 9, 2005, 12:25 PM

Post #2 of 6 (676 views)
Re: [dkhanna01] String manipulation: Fetching all occurances of string [In reply to] Can't Post

one possibility:


Code
my $string = 'TECH01:-abcd,TECH02:-xyz'; 
my $string2 = '';;
($string2 = $string) =~ s/:-[a-z]+//g;
print $string2;


basically its a reverse approach, its removing the occurances of :-abcd :-xyz and leaving only TECH01,TECH02.

This may not work if the example line is not always the same as the one you posted.
-------------------------------------------------


(This post was edited by KevinR on Feb 9, 2005, 12:28 PM)


dkhanna01
Novice

Feb 9, 2005, 8:56 PM

Post #3 of 6 (669 views)
Re: [dkhanna01] String manipulation: Fetching all occurances of string [In reply to] Can't Post

Yes you are correct because the value of string2 is infact

$string2="TECH01:-sdf asfd -sdfasdgf :dsfds,TECH02:-tryt 3454 sdgf"

so the result won't be the same


KevinR
Veteran


Feb 10, 2005, 12:01 AM

Post #4 of 6 (665 views)
Re: [dkhanna01] String manipulation: Fetching all occurances of string [In reply to] Can't Post

Well, this seems to work OK but could probably be improved upon:


Code
#!perl 
use strict;
use warnings;

my @strings = ();
while(<DATA>){
my @temp = ();
push(@temp,$1) while ($_ =~ m/(TECH\d+)/g);
push (@strings,join(",",@temp));
}
print "$_\n" for @strings;

__DATA__
TECH01:-sdf asfd -sdfasdgf :dsfds,TECH02:-tryt 3454 sdgf
TECH01:-sdf -sdfasdgf :dsfds,TECH02:-tryt 3454 sdgf,TECH03:-tryt 3454 sdgf
TECH01:-tryt


prints:


Code
TECH01,TECH02 
TECH01,TECH02,TECH03
TECH01

-------------------------------------------------


dkhanna01
Novice

Feb 10, 2005, 9:41 PM

Post #5 of 6 (660 views)
Re: [KevinR] String manipulation: Fetching all occurances of string [In reply to] Can't Post

Hi Kevin,

Thanks a lot for your support, but I did some small modification in the script actually this script won't work correctly if the DATA itself contain string say "TECH09" or infact "sdfdsTECH09". So in order to work this what did is

push(@temp,$1) while ($_ =~ m/(\bTECH\d+):-/g);

That is the fetch only the string begining with TECH and ends with ":-"

Ne ways Thanks a lot once again.

//DK


davorg
Thaumaturge / Moderator

Feb 11, 2005, 5:18 AM

Post #6 of 6 (657 views)
Re: [dkhanna01] String manipulation: Fetching all occurances of string [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;

while (my $string1 = <DATA>) {
my $string2 = join ',', $string1 =~ /\b(TECH\d+)/g;
print $string2, "\n";
}

__DATA__
TECH01:-abcd,TECH02:-xyz
TECH01:-sdf asfd -sdfasdgf :dsfds,TECH02:-tryt 3454 sdgf
TECH01:-tryt


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives