Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Quick & dirty reasons to love Perl

by greenFox (Vicar)
on Apr 16, 2004 at 04:48 UTC ( [id://345634]=perlmeditation: print w/replies, xml ) Need Help??

I was asked today to extract some data from some html files ("hey Murray you can probably do this pretty quickly with Perl..."). Once I found out it was a definite once off I got to have some fun :)

The html was pretty horrible, lots of tags and no line breaks in the whole file. I slurped it into a scalar and started hacking an re to grab the first wanted bit, thinking I would have three re's, one for each piece of wanted data, but I noticed that the data was basically in pairs with blobs of html in between. So the re's would be simpler if I removed the html first...

A quick super search (I'm lazy) and I had an re to strip html (thanks Juerd++). Then the lights went fully on and less than 30 seconds later I had everything I wanted:

$temp =~ s/<(?:[^>'"]*|(['"]).*?\1)*>/\n/gs; my %temp = grep length, split /\n/, $temp; print "$temp{"Serial Number:"}, $temp{"Model:"}, $temp{"CPU:"}\n";

Yeah I know I was lucky, there were no blank fields, there was an even number of "bits" before the data bits and none after. But it was meant to be a quick and dirty one off :) My colleague was suitably impressed with the speed of the response too!

--
Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Replies are listed 'Best First'.
Re: Quick & dirty reasons to love Perl
by AcidHawk (Vicar) on Apr 16, 2004 at 06:02 UTC

    I think my name should be The Quick, The Dirty and The Ugly.

    I have a boss that will be busy with something and suddenly stop and ask me to whack a script together quickly to do something for him... 7 times out of 10 I come straight to Perlmonks the other 3 times I can basterdise something I have done earlier.. (I keep everything)

    The other day he wanted to test a Windows event log reader and needed to have certain events in the Event Log. A while ago I did a piece that did exactally that in a test so I could incorporate it in my Windows applications for error logging. In 30 secs I had added getop (which I had in another old test script) to the test code and "ta-da-aaa" a script that could take command line args and write them to any of the Windows event logs with the relevant Event ID's etc. etc.

    Solved the bosses problem in no time. This is ONE example but there must be many ...You gotta love something that makes you look soo effecient...

    -----
    Of all the things I've lost in my life, its my mind I miss the most.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://345634]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-20 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found