Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: General Question on parsing a variable

by Athanasius (Abbot)
on Feb 07, 2013 at 05:43 UTC ( #1017560=note: print w/ replies, xml ) Need Help??


in reply to General Question on parsing a variable

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,


Comment on Re: General Question on parsing a variable
Select or Download Code
Re^2: General Question on parsing a variable
by diamondsandperls (Beadle) on Feb 07, 2013 at 16:06 UTC

    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,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017560]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (15)
As of 2015-07-02 17:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls