data sorting in perl
Well this is originally an excel question. Please see this:
http://www.webmaster-forums.com/showthread.php?s=&threadid=16877
Since no one reply for that so I like to compose it in perl(that I got some concept). Now comes.
Question.
text file consist
1
2
.
.
10000
Request
to show numbers ending with 00,15,29,44,33
My try..
#!usr/bin/perl
print "Content-type: text/html\n\n";
@matching_num=qw(00 15 29 44 33)
open(FILE, "$filename");
@all = ;
foreach $line (@all){
chomp $line;
foreach $num (@matching_num){
$line=~ /\d*\$num$/;
}
print "$line\n";
}
close(FILE);
This is unwork and it may be wrong in this line
foreach $num (@matching_num){
$line=~ /\d*\$num$/;
So any kind person can tell me how to do it..
Well do you think it is much more interesting..?
cmix posted this at 18:46 — 14th January 2002.
They have: 5 posts
Joined: Aug 2001
#!usr/bin/perl
print "Content-type: text/html\n\n";
@matching_num=qw(00 15 29 44 33)
open(FILE, "$filename");
@all = ;
close(FILE);
foreach $line (@all){
chomp $line; # not sure about this
foreach $num (@matching_num){
# I think you need an IF statement
if ($line=~ /\d*\$num$/) { print "$line\n"; }
}# end foreach
} # end foreach
cmix posted this at 19:12 — 14th January 2002.
They have: 5 posts
Joined: Aug 2001
After more thought...
I would change this:
if ($line=~ /\d*\$num$/) {
to:
if ($line=~ /^(\d)*($num)$/) {
for better readability, and, just in case ^(\d)* is too greedy, I would change it further to:
if ( ($line=~ /^(\d)*/) && ($line=~ /($num)$/) ) {
print "$line\n";
} # end if
Wil posted this at 20:25 — 14th January 2002.
They have: 601 posts
Joined: Nov 2001
\d+ is going to be far less greedy.
But I would just omit this part altogether. We're assuming that all lines are numbers in the same format. Why just not use
if ($line =~ /\$num$/) {
..
Cheers
- wil
uatt posted this at 23:41 — 14th January 2002.
They have: 27 posts
Joined: Jan 2002
My final solution and making use slternation concept in grep
i.e. $line=~/(00|15|29|44|33)$/; will go further.......
#!usr/bin/perl
print "Content-type: text/html\n\n";
@matching_num=qw(00 15 29 44 33)
$alternate_choice=join ("|", @matching_num);
open(FILE, "$filename");
@all = ;
while (@all){
chomp ;
if(/(\D)/){print"It should input numbers";exit 0;}
else{print "$_\n" if /($alternate_choice)$/;}
}
close(FILE);
Thanks for everyone's response...
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.