Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old December 9th, 2000, 03:11 PM
Pepe Pepe is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 71 Pepe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Hi !

What am I trying to do is extracting all six-digit numbers from html and writing them to another array (@numbers). Html document is contained in @messages array.

This is what I tried to do and it didn't work:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
sub output_status {
my (@messages) = @_;
my $message;
my @numbers = ("Start");
foreach $message (@messages) {
if ($message =~ /d{6}/) {
$broj = ($message =~ /(d{6})/);
push (@numbers,$broj);
}
}
print "Content-type: text/htmlnn";
foreach $line (@numbers) {
print "Number: $line<BR>";
}
}
[/code]
Output of this code looks like this:

Number: Start
Number: 1


But there is lots of six-digit numbers on html page which should be printed

[This message has been edited by Pepe (edited December 09, 2000).]

Reply With Quote
  #2  
Old December 9th, 2000, 08:21 PM
Pepe^ Pepe^ is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 0 Pepe^ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have found way to go around this by splitting html code to rows and I used </TR> as delimiter. But I have felling that there is much simpler way to do it.
Now it works like this:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
sub output_status {
my (@messages) = @_;
my $message = $messages[0];
@pairs = split(/</TR>/, $message);
my @numbers = ("Start");
foreach $message (@pairs) {
if ($message =~ /d{6}/) {
$message =~ /(d{6})/;
push (@numbers,$1);
}
}
print "Content-type: text/htmlnn";
foreach $line (@numbers) {
print "Number: $line<BR>";
}
}

[/code]
Could someone please tell me how to extract specific string from one array and push it to another?

Reply With Quote
  #3  
Old December 9th, 2000, 11:08 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
In your code, you're only fetching the first match. You need to use a loop with the 'g' option to fetch all matches:

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
sub output_status {
my @messages = @_;
my @numbers = ("Start");

for (@messages) {
chomp;
push(@numbers,$1) while /(d{6})/g;
}

print "Content-type: text/htmlnn";
for (@numbers) {
print "Number: $_<br>n";
}
}
[/code]


[This message has been edited by vpopper (edited December 09, 2000).]

Reply With Quote
  #4  
Old December 10th, 2000, 12:35 PM
Pepe^ Pepe^ is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 0 Pepe^ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks. I have one more problem: How do I exclude some 6-digit numbers from that loop. I'm trying to prevent these numbers to be pushed to new array "000000" "666666" "501117" (html color code)

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
for (@messages) {
chomp;
push(@numbers,$1) while /(d{6})/g;
}

[/code]


[This message has been edited by Pepe^ (edited December 10, 2000).]

[This message has been edited by Pepe^ (edited December 11, 2000).]

Reply With Quote
  #5  
Old December 10th, 2000, 05:27 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Pepe^:
I have one more problem: How do I exclude some 6-digit numbers from that loop. I'm trying to prevent these numbers to be pushed to new array "000000" "666666" "501117" (unwanted part of html file)[/quote]

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

%skip = map { $_ => 1 } qw(000000 666666 501117);

for (@messages) {
chomp;
push(@numbers,$1) while /(d{6})/g and not $skip{$1};
}

[/code]


Reply With Quote
  #6  
Old December 10th, 2000, 05:36 PM
Pepe^ Pepe^ is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 0 Pepe^ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I changed this code but now nothing gets pushed to @numbers array

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Parsing matched string...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway