Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PERL: HTML Informationen kürzen

Empfohlene Antworten

Hallo liebe Forenmitglieder,

ich hänge leider gerade an einer, wohl einfachen, Aufgabe. Ich habe bei mir zu Hause einen Arduino, samt Ethernet-Shield. Das Ethernet-Shield gibt analoge Daten auf einer simplen HTML Seite aus.


<!DOCTYPE HTML>
<html>
<meta http-equiv="refresh" content="5">
analog input 0 is 315<br />
</html>
[/PHP]

Nun ist es aber so, das ich nur die Zahl weiter benutzen möchte und anhand derer eine Ausgabe produzieren möchte. Ist die Zahl über 512 "ok" und unter 512 "warnung".

So sieht derzeit mein Code aus.

[PHP]
#!/usr/bin/perl
my $url = '192.168.178.26';
# Holt Informationen von Website

use LWP::Simple;
my $content = get $url;
die "Couldn't get $url" unless defined $content;

print $content
Inhalt durchsuchen

if($content =~ 'analog input 0 is([0-9]+)') {
print "Arduino laeuft.";
} else {
print "Fresh Air is apparently jazzless today.\n";
}

Vielen Dank schonmal :)

Ganz allgemein: gewöhne Dir bei Perl am besten an, jedes Skript mit

use strict;
use warnings;[/PHP]

zu eröffnen. Diese Pragmas unterstützen Dich dabei, Syntaxfehler schneller zu finden.

Aber auch ohne die Pragmas ist schon einmal der folgende Syntaxfehler offensichtlich:

[code] print $content Inhalt durchsuchen [/code]

Zum Regex:

[PHP]$content =~ 'analog input 0 is([0-9]+)'

Vor der öffnenden Klammer fehlt ein Leerzeichen - der Regex kann also nicht treffen. Alle "Matches" in runden Klammern werden in den Spezialvariablen $1, $2, $3 usw. gespeichert - der Inhalt der ersten runden Klammern landet also in $1. Dies kannst Du dann weiterverarbeiten.

Kleines Testskript:

#!/usr/bin/perl
use strict;
use warnings;

my $content = <<__END__;
<!DOCTYPE HTML>
<html>
<meta http-equiv="refresh" content="5">
analog input 0 is 315<br />
</html>
__END__
if($content =~ 'analog input 0 is (\d+)')
{
print "Arduino laeuft.\n";
print $1 > 512 ? "ok\n" : "warning\n";
}
else
{
print "Fresh Air is apparently jazzless today.\n";
}[/PHP]

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.