Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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
[Your Mother]: They would think so.
[jdporter]: ok, I need a recipe for piping lines "through" an external program which is itself a filter
[jdporter]: without using a tmp file
[1nickt]: tobyink perl -MTypes::Standard= is_Int -Mstrict -wE 'say 1 if is_Int 1.0'
[jdporter]: so that I can use the existing expand unix util. Otherwise, I'll probably use Text::Tabs.
[1nickt]: pryrt I guess I don;t really care if user 42 logs on as 42.0 ... more of an academic question at this point.
[LanX]: jdporter: open PIPE,'-|' ?
[LanX]: oh you want the result line by line?
[jdporter]: ok, LanX, then what?
[jdporter]: It doesn't have to be line by line. Just "my program" "writes" to the external prog and also/then "reads" from it.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2017-05-24 20:27 GMT
Find Nodes?
    Voting Booth?