Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Changing the order of elements in the variable

by Shaveta_Chawla (Acolyte)
on May 30, 2012 at 06:19 UTC ( #973194=perlquestion: print w/ replies, xml ) Need Help??
Shaveta_Chawla has asked for the wisdom of the Perl Monks concerning the following question:

$author = "Smith, Kristin A.; Shirley, Mark Harper ; Saff, David ; Ernst, Michael D.; Patrick, Nicholas J. M ; Yu, (Jeffrey) Hu; De La Macorra, F ; Smith, Meghan. ";

this variable has author names , but my target is to change the order of individual author, like "Smith, Kristen A." should change to "Kristen A. Smith".

I have used the following code::
@author = split(';', $author); $ca = scalar(@author); for (my $i=0; $i<$ca ; $i++) { ($a, $b) = split(',', $author[$i]); $author[$i] = "$b$a"; } $author = join(',', @author); print "$author\n";

i find this code really complex. Please suggest a better code using map function.

Comment on Changing the order of elements in the variable
Download Code
Re: Changing the order of elements in the variable
by moritz (Cardinal) on May 30, 2012 at 06:59 UTC
      That's fantastic.. But one comma (,) is missed- it seems.
      $author =~ s/([^,;]+),([^,;]+)/$2, $1/g;

        You can check this sort of thing on the commandline: usually advisable if you're going to tell someone they got something wrong.

        Shaveta_Chawla wants: "... "Smith, Kristen A." should change to "Kristen A. Smith".".

        moritz' solution does this:

        $ perl -Mstrict -Mwarnings -E 'my $author = q{Smith, Kristen A.}; $aut +hor =~ s/([^,;]+),([^,;]+)/$2 $1/g; say $author' Kristen A. Smith

        Your correction does not do this:

        $ perl -Mstrict -Mwarnings -E 'my $author = q{Smith, Kristen A.}; $aut +hor =~ s/([^,;]+),([^,;]+)/$2, $1/g; say $author' Kristen A., Smith

        -- Ken

Re: Changing the order of elements in the variable
by choroba (Abbot) on May 30, 2012 at 07:05 UTC
    I am not sure it is any "better". Even a smaller improvement (e.g. using for (@author) { ... } instead of the C-style loop can please someone.
    #!/usr/bin/perl use warnings; use strict; my $author = 'Smith, Kristin A.; Shirley, Mark Harper ; Saff, David ; +Ernst, Michael D.; Patrick, Nicholas J. M ; van der Loede, Greg; Yu, +(Jeffrey) Hu; De La Macorra, F ; Smith, Meghan. '; $author =~ s/.\s*$//; print((join ', ', map { join ' ', reverse split / *, */; } split / *; */, $author), '.');
Re: Changing the order of elements in the variable
by uday_sagar (Scribe) on May 30, 2012 at 07:13 UTC

    Your had almost reached, but I see that you have got a slight confusion between comma (,) and semicolon (;)

    These are the slight changes to your code:

    line 6, $author\[$i\] = "$b, $a"; line 9, $author = join(';', @author);

    Thats it! :-)

Re: Changing the order of elements in the variable
by MidLifeXis (Prior) on May 30, 2012 at 12:42 UTC

    A note on this section of code:

    ($a, $b) = split(',', $author[$i]);

    It is often recommended not to use $a and $b, as they are special-cased in some functions within perl (sort, for example). You could get some funky results if you collide with one of these cases. "Better" names might be $lastName and $firstNameAndInitial or $partBeforeComma and $partAfterComma.

    You could also trim off the space along with the comma with: split(',\s+', ...), but then you need to also adjust your assignment in the next line.

    --MidLifeXis

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2014-09-19 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls