Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Grey Fox,
Glad you got some good ideas from this thread!

I thought something was weird with the missing semicolon - just a small detail.

As far as sorting goes print "$name\n"; #push to DB or whatever here... is where you can just push to a list like @full_names. Here is one way of many to do the sort. Note that the "Boop" family winds up in the correct sort order.

#!/usr/bin/perl -w use strict; my @names = ( "Builder,Bob", "Stein,Franklin", "Boop,Elizabeth", "Boop,Albert", "Bear,Izzy", "SomeGuy,Guy", "Einstein,Albert",); @names = sort by_last_name @names; print join("\n",@names),"\n"; sub by_last_name { my ($a_last, $a_first) = split (/,/,$a); my ($b_last, $b_first) = split (/,/,$b); $a_last cmp $b_last or $a_first cmp $b_first } __END__ Prints: Bear,Izzy Boop,Albert Boop,Elizabeth Builder,Bob Einstein,Albert SomeGuy,Guy Stein,Franklin
I added the simple code to sort by first name.
#!/usr/bin/perl -w use strict; my @names = ( "Builder,Bob", "Stein,Franklin", "Boop,Elizabeth", "Boop,Albert", "Bear,Izzy", "SomeGuy,Guy", "Einstein,Albert",); @names = sort by_last_name @names; print join("\n",@names),"\n"; ## Now print sorted by first name ## print "\nNow sorted by first name\n"; @names = sort by_first_name @names; foreach my $full_name (@names) { my ($last,$first) = split (/,/,$full_name); print "$first,$last\n"; } sub by_last_name { my ($a_last, $a_first) = split (/,/,$a); my ($b_last, $b_first) = split (/,/,$b); $a_last cmp $b_last or $a_first cmp $b_first } sub by_first_name { my ($a_last, $a_first) = split (/,/,$a); my ($b_last, $b_first) = split (/,/,$b); $a_first cmp $b_first or $a_last cmp $b_last } __END__ Prints: Bear,Izzy Boop,Albert Boop,Elizabeth Builder,Bob Einstein,Albert SomeGuy,Guy Stein,Franklin Now sorted by first name Albert,Boop Albert,Einstein Bob,Builder Elizabeth,Boop Franklin,Stein Guy,SomeGuy Izzy,Bear

In reply to Re^3: How do I combine SPLIT with trimming white space by Marshall
in thread How do I combine SPLIT with trimming white space by Grey Fox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found