justuptime.com - monitor your servers & websites

data sorting in perl

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

They have: 27 posts

Joined: Jan 2002

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..?

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

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

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

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...