Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

General Question on parsing a variable

by diamondsandperls (Beadle)
on Feb 06, 2013 at 20:51 UTC ( #1017502=perlquestion: print w/replies, xml ) Need Help??
diamondsandperls has asked for the wisdom of the Perl Monks concerning the following question:

I have a variable that has many instances of stored data in it like the below..the beginning is storeId of each data entry the variable has and an ending of end I have many instances but only want to display the matches and the full data snapshot if criteria in the middle is matched then print the variable.

storeId = 111
a bunch of randomness
mysearch string criteria i want to match and only print this subset data instead of other instances
more randomness
end

Replies are listed 'Best First'.
Re: General Question on parsing a variable
by moritz (Cardinal) on Feb 06, 2013 at 20:56 UTC
Re: General Question on parsing a variable
by Athanasius (Chancellor) on Feb 07, 2013 at 05:43 UTC

    You will find it easier to access the records if you read them into a suitable data structure first. In Perl, that usually means a hash:

    my %hash; $hash{$1} = $2 while $var =~ /storeID = (\d+)\s+(.*?)end/gs;

    Then you can search like this:

    my $criterion = ...; print "storeID = $_\n", $hash{$_} for grep { $hash{$_} =~ /$criterion/ + } keys %hash;

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      http://scsys.co.uk:8002/228600

      I have many entries like the ones presented stored in a variable $var I only want to print the entries that have stockStatus = In stock or stockStatus = Limited Stock ignoring case though on the match so i know /i and a multiline match

        It seems each record now ends with a blank line, rather than the word “end” as in the OP. And each record is a collection of lines of the form key = value. So we add another loop, and create a hash in which each key is the storeId (as before), but each value is a (reference to a) nested hash of key/value pairs:

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: General Question on parsing a variable (what)
by Anonymous Monk on Feb 07, 2013 at 08:27 UTC
    What is your question?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017502]
Approved by Corion
help
Chatterbox?
[GotToBTru]: late to the conversation .. but thanks, Corion, for the reminder about Perl::Tidy
[Corion]: GotToBTru: Heh ;) I'm not really endorsing it, as I found that any gains made through it are easily squandered by the hours spent on configuring it. At least in my case :)
[GotToBTru]: Like most Swiss Army Knives, you can cut yourself trying to find the blade you actually want
[GotToBTru]: i am satisfied with the default settings turning the seemingly random formatting into something reliable
[Corion]: GotToBTru: Yeah, I started a quick(ly implemented but slowly running) implementation of a program that would guess the "best" configuration (that is, least amount of changes) from my code, but then found that it stops parsing the source as soon as it ..
[GotToBTru]: sadly, it can't do anything about this
[Corion]: ... encounters sub foo($bar,$baz) {, which is something I like nowadays

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-02-27 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (387 votes). Check out past polls.