Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to parse a scalar variable

by cajun (Chaplain)
on Jun 05, 2005 at 05:49 UTC ( [id://463656]=perlquestion: print w/replies, xml ) Need Help??

cajun has asked for the wisdom of the Perl Monks concerning the following question:

I recently needed to parse the first few lines of some fairly large files. During my research in trying to figure out how to read just a few lines from a file, then move on to the next file, I found Jumping out of a partially read file.

This node solve my quest for how to read just a few lines from each file. However it left me with a different question. While I'm very accustomed to opening a file and processing it line by line, I've never done this with a scalar variable (ref: $header in Jumping out of a partially read file.

I'm fairly sure the answer is simple, however in many searches on Perlmonks.org as well as Google, and of course "Learning Perl", the solution has not yet become clear.

My $header scalar contains data that looks like this:

X-Account-Key: account3 Return-Path: <> Delivered-To: somename@somedomain.com Received: (qmail 77369 invoked for bounce); 3 Jun 2005 06:35:09 -0000 Date: 3 Jun 2005 06:35:09 -0000 From: MAILER-DAEMON@smtp.somedomain.com To: somename@somedomain.com Subject: failure notice
Thanks for any suggestions,
Mike

Replies are listed 'Best First'.
Re: How to parse a scalar variable
by Zaxo (Archbishop) on Jun 05, 2005 at 06:08 UTC

    Mail::Header may be of interest.

    If you want to parse this on your own, try paragraph mode ($/ = '';) and read the first "line" to get all the headers. You can then split on newlines to get the lines and on colon to get header tags and content.

    my $headers = do { local $/ = ''; scalar <$fh>; }; my %header = map { map { join ' ', split; } split /: /, $_, 2; } split /\r?\n\r?/, $headers;
    You may need to fiddle with the newline split, but I tried to make it cover the possibilities. The scalar operator isn't really necessary before the diamond read, but it documents intent. The inner map just trims and normalizes whitespace, and may be omitted.

    After Compline,
    Zaxo

Re: How to parse a scalar variable
by jZed (Prior) on Jun 05, 2005 at 05:57 UTC
    Parsing lines in a file is essentially treating the file as an array of lines. To treat a scalar as an array of lines, split it on newlines. Something like this:
    for my $line(split /\n/,$header){ # process $line as though it came from a <> }
Re: How to parse a scalar variable
by sh1tn (Priest) on Jun 05, 2005 at 10:57 UTC
    my$start = 0;# from 1st my$stop = 2;# to 3rd line for( (split /\n/, $header)[$start..$stop] ){ print $_,$/ }


Re: How to parse a scalar variable
by NateTut (Deacon) on Jun 05, 2005 at 15:14 UTC
    Depending upon what you are looking for in the scalar, you could just use a regex to pull out what you need.
Re: How to parse a scalar variable
by cajun (Chaplain) on Jun 06, 2005 at 01:46 UTC
    Many thanks to all, jZed Zaxo sh1tn NateTut.

    I especially appreciate the several choices of ways to accomplish this task. That is extremely helpful.

    Thanks to all for the great suggestions.

    Mike

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-25 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found