|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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).] |
|
#2
|
|||
|
|||
|
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? |
|
#3
|
||||
|
||||
|
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).] |
|
#4
|
|||
|
|||
|
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).] |
|
#5
|
||||
|
||||
|
<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] |
|
#6
|
|||
|
|||
|
I changed this code but now nothing gets pushed to @numbers array
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Parsing matched string... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|