Okay, then, are you familiar with for/foreach? Once you've started the
loop, it's essentially the same as your original while().
my @array = <STDIN>;
foreach(@array){
do your name sort with $_...
}
@array = sort @array;
(See below if this doesn't click for you, I haven't tested them, but
something similar should work.)
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
use strict;
use warnings;
open(INFILE,"myfile.txt") || die "Could not open \"myfile.txt\"!";
my @array = <INFILE>;
foreach(@array){
chomp $_;
$_ =~ /^(\w+)\s+(\w+)$/;
$_ = "$2,$1";
}
@array = sort @array;
print @array;
use strict;
use warnings;
open(INFILE,"myfile.txt") || die "Could not open \"myfile.txt\"!";
my @array = <INFILE>;
foreach(@array){
chomp $_;
($fname,$lname) = split / /,$_;
$_ = "lname,fname";
}
@array = sort @array;
print @array;
-----Original Message-----
From: Grant Hansen [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 6:29 PM
To: Timothy Johnson
Cc: Perl Beginners
Subject: Re: Help on Arrays - Beginner
I thought about that and I understand how that works, however the piece that
I
am missing and don't understand is how to access each element of the array,
reverse the data and put the comma in. I know how to access the elements,
it
is the manipulating and putting back into the array for further processing
that I am confused on.
Thanks
On Friday 20 September 2002 08:06 pm, Timothy Johnson wrote:
> I'll give you a hint. This is perfectly legal:
>
> my @array = <STDIN>;
>
> Let me know if you still can't figure it out.
>
> -----Original Message-----
> From: Grant Hansen [mailto:[EMAIL PROTECTED]]
> Sent: Friday, September 20, 2002 6:03 PM
> To: [EMAIL PROTECTED]
> Subject: Help on Arrays - Beginner
>
>
> Hello All,
>
> I am new to Perl and I will admit up front this is for a Perl course.
> However, the course is online and thus timely communication from the
> instructor is not always possible, so I am hoping someone will steer me in
> the right direction.
>
> The assignment has to do with taking a file, and reading it to <STDIN>,
> modifying each line, sort and then print.
>
> Here is the Input:
>
> Grant Hansen
> Dave Thomas
> Roger Starbauch
>
> Here is the intended output:
>
> Hansen, Grant
> Starbauch, Roger
> Thomas, Dave
>
> Here is my code:
>
> while(<STDIN>) {
> chomp(@lines = split);
> @lines1 = $lines[1] . ", " . $lines[0];
> @lines2 = sort (@lines1);
> print "@lines2 \n";
> }
>
> My code is not sorting correctly and I know why (I sort after each line of
> input), I just don't know how to fix it.
>
> Any help is appreciated.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]