Sorting a String

Sorting a String

am 16.08.2011 18:04:10 von matt

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?

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

Re: Sorting a String

am 16.08.2011 18:06:48 von oleber

sort like string or like numbers?

On Tue, Aug 16, 2011 at 18:04, Matt wrote:
> 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. =A0I need it sorted.
> =A0How would I approach this?
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>



--=20
Marcos Rebelo
http://www.oleber.com/
Webmaster of http://perl5notebook.oleber.com

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

RE: Sorting a String

am 16.08.2011 18:09:35 von David.Wagner

>-----Original Message-----
>From: Matt [mailto:lm7812@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 <=3D> $b} split(/\n/, $a_string) ) {
# assuming numeric sort or if alpha, change <=3D> cmp
# can do what you want with it at this point.
}

         If you have any questions and/or problems, =
please let me know.=20
        =A0Thanks.=20
=A0
Wags ;)=20
David R. Wagner=20
Senior Programmer Analyst=20
FedEx Services=20
1.719.484.2097 Tel=20
1.719.484.2419 Fax=20
1.408.623.5963 Cell
http://Fedex.com/us



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


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

Re: Sorting a String

am 16.08.2011 18:19:24 von Brandon McCaig

On Tue, Aug 16, 2011 at 12:04 PM, Matt wrote:
> 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 sorte=
d.
>  How would I approach this?

Assuming you want to sort the lines, you could split it into a list,
store the list in an array, sort the array, and the join the array
back into a string:

use strict;
use warnings;

my $foo =3D < 4565 line1
2345 line2
500 line3
EOF

my @bar =3D split "\n", $foo;

@bar =3D sort @bar;

$foo =3D join "\n", @bar;

__END__

Of course, that will use the standard string comparison on the entire
line. If you want to sort by the number in front, for example, then
you'll have to specify the code to sort with:

sub get_baz
{
my $record =3D shift;

return ($record =3D~ /^([0-9]+)/);
}

@bar =3D sort { get_baz($a) <=3D> get_baz($b) } @bar;

__END__

HTH,


--=20
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software ..org>

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

Re: Sorting a String

am 16.08.2011 18:52:34 von jwkrahn

Matt wrote:
> I believe you can sort an array like so:
>
> sort @my_array;

That should be:

@my_array = 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?

$a_string = join '', sort $a_string =~ /.*\n/g;




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

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

Re: Sorting a String

am 17.08.2011 17:44:04 von Shlomi Fish

Hi,

On Tue, 16 Aug 2011 11:09:35 -0500
"Wagner, David --- Sr Programmer Analyst --- CFS"
wrote:

> >-----Original Message-----
> >From: Matt [mailto:lm7812@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 <=3D> $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 <=3D> cmp
> # can do what you want with it at this point.
> }
>=20
>          If you have any question=
s and/or problems, please let me know.=20
>          Thanks .=20
>  
> Wags ;)=20
> David R. Wagner=20
> Senior Programmer Analyst=20
> FedEx Services=20
> 1.719.484.2097 Tel=20
> 1.719.484.2419 Fax=20
> 1.408.623.5963 Cell
> http://Fedex.com/us
>=20
>=20
>=20
> >--
> >To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> >For additional commands, e-mail: beginners-help@perl.org
> >http://learn.perl.org/
> >
>=20
>=20



--=20
------------------------------------------------------------ -----
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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Sorting a String

am 18.08.2011 01:25:09 von jwkrahn

Shlomi Fish wrote:
>
> "Wagner, David --- Sr Programmer Analyst --- CFS"
> wrote:
>>
>> 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.

It will also work without the /m option.

split /^/, $a_string



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

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