Hi,

On Tue, 16 Aug 2011 11:09:35 -0500
"Wagner, David --- Sr Programmer Analyst --- CFS" <david.wag...@fedex.com>
wrote:

> >-----Original Message-----
> >From: Matt [mailto:lm7...@gmail.com]
> >Sent: Tuesday, August 16, 2011 10:04
> >To: beginners@perl.org
> >Subject: Sorting a String
> >
> >I believe you can sort an array like so:
> >
> >sort @my_array;
> >
> >I need to sort a string though.
> >
> >I have $a_string that contains:
> >
> >4565 line1
> >2345 line2
> >500 line3
> >etc.
> >
> >Obviously \n is at end of every line in the string.  I need it sorted.
> > How would I approach this?
> >
>    Since a \n is at end, then could use split like:
>       for my $dtl ( sort {$a <=> $b} split(/\n/, $a_string) ) {

One can also do split(/^/m, $a_string) to split into lines while preserving the
newlines.

Regards,

        Shlomi Fish

>               # assuming numeric sort or if alpha, change <=> cmp
>               # can do what you want with it at this point.
>        }
> 
>          If you have any questions and/or problems, please let me know. 
>          Thanks. 
>  
> Wags ;) 
> David R. Wagner 
> Senior Programmer Analyst 
> FedEx Services 
> 1.719.484.2097 Tel 
> 1.719.484.2419 Fax 
> 1.408.623.5963 Cell
> http://Fedex.com/us
> 
> 
> 
> >--
> >To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> >For additional commands, e-mail: beginners-h...@perl.org
> >http://learn.perl.org/
> >
> 
> 



-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
List of Portability Libraries - http://shlom.in/port-libs

Become an awesome Perl ninja rockstar vampire zombie pirate.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to