Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

String Search

by kallol.chakra (Initiate)
on Aug 31, 2009 at 09:02 UTC ( #792326=perlquestion: print w/replies, xml ) Need Help??
kallol.chakra has asked for the wisdom of the Perl Monks concerning the following question:


I am having a problem.

Lets take this scenario.

The Input data is as follows:

header { subheader1 { field1 : value field2 : value field3: value } subheader2 { subheader3 { field4 : value field5 : value field6: value } subheader4 { field7 : value field8 : value field9: value } } }
header { subheader1 { field1 : value1 field2 : value1 field3: value1 } subheader2 { subheader3 { field4 : value1 field5 : value1 field6: value1 } subheader4 { field7 : value1 field8 : value1 field9: value1 } } }

The output will be :

field1,field2,field3,field4,field5,field6,field7,field8,field9 value,value,value,value,value,value,value,value,value value1,value1,value1,value1,value1,value1,value1,value1,value1

Here main problem is that: The value os field2 or field3 may be missing then the output may be:

field1,field2,field3,field4,field5,field6,field7,field8,field9 value,value,value,,value,value,value,value,value value1,value1,value1,value1,value1,value1,,value1,value1

In the above output The first line field4 is missing and in the second line field7 is missing

How Can I get the output can you please help me.

Replies are listed 'Best First'.
Re: String Search
by moritz (Cardinal) on Aug 31, 2009 at 09:05 UTC
    What have you tried so far? Show us your code, without seeing it we can't correct any mistakes you might have made.
    Perl 6 - links to (nearly) everything that is Perl 6.

      Can you please look into it?

      use IO::File; open (INPUT_FILE, $ARGV[0]) || die "cant open file\n"; my @body = <INPUT_FILE>; my $count= 0; $no_of_lines=scalar(@body); for ( $i = 0 ; $i < $no_of_lines ; $i++) { $variable = $body[$i]; if ( $variable =~ "field1") { print "$field1,"; } if ( $variable =~ "field2") { print "$field2,"; } if ( $variable =~ "field2") { print "$field2,"; } ... ... } close file;

      It is a very bad way I have written, but main problem is that I can not write any else in the loop. I have to track whether any field1 or field2 is missing. The logic is : 1. Match -> field1:value1 2. If field1 exists then print value1, 3. else 4. print null,

        This code doesn't produce the output you say it does. Could you please enlighten me why? Does it really do that for you? is the part that you omitted (and replaced with ...) important?
        Perl 6 - links to (nearly) everything that is Perl 6.
Re: String Search
by bichonfrise74 (Vicar) on Aug 31, 2009 at 19:11 UTC
    I think you might want to consider a hash of hash data structure. In that way, you can have a finer control of your data and you can do whatever you like for the missing data as well.


      can you please allow me to know the details logic of hash of hash data structure and also finder control.

      I think Hash Datastructure traverse the data from leaf to root of a tree. How can I configure the data in C. In C I can do link list of structures. then it will be very easy to find the data. Can you please help me here?


Re: String Search
by Anonymous Monk on Aug 31, 2009 at 09:09 UTC
    What is the name of that format? Is it JSON or YAML?

      Hi, I can not understant the format.. Can you please help me? Regards

        I am trying to help you, what is the name of this format? Where did you get the sample from? It looks like broken JSON or YAML.

        The format is a System Specific Format.. I have to implement a logic to extract the data..

Re: String Search
by Anonymous Monk on Sep 03, 2009 at 12:18 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://792326]
Approved by moritz
[choroba]: Dangers of WFH: a mailman rings, brings a parcel for my wife, I need to go outside, take my son with me, sign a paper *and* into the postman's mobile app, get back. Guess what the soup was doing meanwhiles?
[Discipulus]: soup without heat protections as CPUs are doomed to burned..
[1nickt]: The dangers of milk-based soup, you mean?
[Corion]: choroba: Ouch! But you don't need that many external events for that. I have on several occasions set up milk to cook and then programmed a bit, only to find the milk burned and congealed over the stove :-/
[choroba]: it's still edible, just need to scour the pot
[1nickt]: Corion I have a large site I need to check for broken links and absolute links. Making a scraper is easy of course; a spider that crawls a whole site is a little more involved ... I was planning a queue-based tool. Intersted to see what you do...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2017-10-18 11:39 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (244 votes). Check out past polls.