Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Schwartzian transform and split 1

Status
Not open for further replies.

JimJx

Technical User
Joined
Feb 16, 2001
Messages
202
Location
US
Hi all,

for some reason, I am having a problem with this.

I have a flat file in the form of:

$userID\t$TempPass\tgroup\n

What I need to do is first, sort everything by userID and the Schwartzian gives output like

User1 temppass1 new
User10 temppass1 new
User2 temppass1 new
User20 temppass1 new
User3 temppass1 new

My userIDs are all that format, User followed by a number.

Then I run the split, and get the same thing, meaning I get the complete line. No split at all so I can pull out the userID

I am sure I am doing something stupid here, my logic may be all wrong....

As always, suggestions greatly appreciated.

Jim

My code:

Code:
open(PASSWORD, "$PASSWORD_FILE") || &Error("Cannot open password file: $PASSWORD_FILE, Error: $!",1);
	@PassFile = <PASSWORD>;
close(PASSWORD);

my @sorted = map  {$_->[0]}
             sort {$a->[1] cmp $b->[1] ||
                   $a->[2] <=> $b->[2]}
             map  {m/^([a-z]+)(\d+)$/i; [$_,lc($1),$2]}  @PassFile;

$cnt = 0;
foreach (@sorted) {
	($name, $pw, $level) = split('\t');
	 print "<TR><TD ALIGN=\"CENTER\"><INPUT TYPE=radio Name='No' Value='$cnt'> </TD><TD> $name </TD></TR>\n";
$cnt++;
}
 
here:

Code:
m/^([a-z]+)(\d+)$/

you should really remove the end of string anchor from the regexp, so it should be:

Code:
m/^([a-z]+)(\d+)/

that might help, but it's hard to say without seeing the real data file. You might be better off spliting on spaces intsead of tabs:

($name, $pw, $level) = split(/\s+/);

tabs can be finicky at times. You should also be using "strict" in your perl scripts.

- Kevin, perl coder unexceptional!
 
Does the script know what to split, using that syntax?

Yes it will know. Perl will defualt to using $_ in many functions if no other variable is used. In this case:

Code:
foreach (@sorted) {

is using $_ as the default scalar $_ to loop through the array so the split is splitting the correct scalar.

- Kevin, perl coder unexceptional!
 
Excellent, Kevin.

All your suggestions work perfectly.

Thank you and have a star!
Jim
 
Thanks fo the star. Just 8 more to be the first perl person to reach 300 stars in the perl forum.

[king] [cheers]

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top