Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Author separation

by Shaveta_Chawla (Acolyte)
on Jun 07, 2012 at 07:41 UTC ( #974896=perlquestion: print w/ replies, xml ) Need Help??
Shaveta_Chawla has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i have a string $author = "Cheng, Liying, Fox, Janna., and Zheng, Ying. ";
i want o/p like
$author = "Liying Cheng, Janna. Fox, Ying. Zheng ";
please guide!!

Comment on Author separation
Re: Author separation
by kcott (Abbot) on Jun 07, 2012 at 08:08 UTC
Re: Author separation
by DrHyde (Prior) on Jun 07, 2012 at 09:49 UTC
    That's easy.
    if($author eq "Cheng, Liying, Fox, Janna., and Zheng, Ying. ") { $author = "Liying Cheng, Janna. Fox, Ying. Zheng "; }
    If you want something that works in the general case, then I suggest that you start by showing us what you've tried.
      I have used this code:
      $author = "Cheng, Liying, Fox, Janna., Zheng, Ying."; @author = split(',' , $author); $length = scalar(@author); $l = 0; $j = 1; for($i=0; $i<$length/2 ; $i++) { $author1[$i]= "$author[$j] $author[$l]"; $l = $l+2; $j = $j+2; } $author = join (',' , @author1); print "$author\n\n";
      but this is not feasible
Re: Author separation
by Shaveta_Chawla (Acolyte) on Jun 07, 2012 at 11:05 UTC
    This code can also be used::
    $author = "$author,"; $author =~ s/(.*?),(.*?),/$2 $1,/g;
Re: Author separation
by cdarke (Prior) on Jun 07, 2012 at 13:05 UTC
    Many ways of doing this, here is mine:
    use strict; use warnings; my $author = "Cheng, Liying, Fox, Janna., and Zheng, Ying. "; $author =~ s/\.| and| //g; my %hash = split (',', $author); my @authors; for my $key (sort keys %hash) { push @authors,"$hash{$key} $key"; } $author = join(',', @authors); print "$author\n";
    Gives:
    Liying Cheng,Janna Fox,Ying Zheng

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://974896]
Approved by betterworld
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-08-29 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls