Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Best way to sort these strings ? recursive way ? use Parse::RecDescent?

by bertigo (Novice)
on Oct 01, 2005 at 09:52 UTC ( [id://496616]=perlquestion: print w/replies, xml ) Need Help??

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

my aim is to sort these strings representing jobs (extract from TNG scheduler):
JOB ID=EX1J DESC='JOB1'
JOB ID=EX2J DESC='JOB2'
JOB ID=EX3J DESC='JOB3'
JOB ID=EX4J DESC='JOB3'
EX4J PRED EX2J
EX3J PRED EX1J
EX4J PRED EX1J
in order to sort in human way:
EX1J    desc JOB1
EX2J    desc JOB2
EX3J    desc JOB3    PRED EX1J
EX4J    desc JOB3    PRED EX1J, EX2J
thanks for answering to my first post ..
  • Comment on Best way to sort these strings ? recursive way ? use Parse::RecDescent?

Replies are listed 'Best First'.
Re: Best way to sort these strings ? recursive way ? use Parse::RecDescent?
by sauoq (Abbot) on Oct 01, 2005 at 10:37 UTC
    Best way to sort these strings ? recursive way ? use Parse::RecDescent?

    I don't see anything recursive about it and this is certainly not a job for Parse::RecDescent.

    Have you actually tried anything here? Got code?

    I'd approach this by building a data structure from all the input before outputting it. Knowing nothing more about your input than the sample you've provided, I'd use a structure that looked like this when complete:

    { 'EX1J' => { 'desc' => 'JOB1' 'pred' => [], }, 'EX2J' => { 'desc' => 'JOB2' 'pred' => [], }, 'EX3J' => { 'desc' => 'JOB3' 'pred' => ['EX1J'], }, 'EX4J' => { 'desc' => 'JOB3' 'pred' => ['EX1J', 'EX2J'], }, }
    The code to do that would be pretty straight forward. Is this enough to get you started?

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Best way to sort these strings ? recursive way ? use Parse::RecDescent?
by ikegami (Patriarch) on Oct 01, 2005 at 15:00 UTC
    sauoq is right, P::RD is not needed here, just a pair of regular expressions. Specifically,
    my %data; while (<DATA>) { chomp; if (/^JOB ID=(\S+) DESC='(\S+)'/) { my $id = $1; my $desc = $2; $data{$id} = { desc => $desc, pred => [], }; } else { my ($id, $pred) = (split(/\s+/))[0, 2]; push(@{$data{$id}{pred}}, $pred); } } require Data::Dumper; print(Data::Dumper::Dumper(\%data)); __DATA__ JOB ID=EX1J DESC='JOB1' JOB ID=EX2J DESC='JOB2' JOB ID=EX3J DESC='JOB3' JOB ID=EX4J DESC='JOB3' EX4J PRED EX2J EX3J PRED EX1J EX4J PRED EX1J
Re: Best way to sort these strings ? recursive way ? use Parse::RecDescent?
by chester (Hermit) on Oct 01, 2005 at 16:39 UTC
    I just learned Parse::RecDescent yesterday and I wanted to try it. Consider this an example of how not to solve the problem.

    use warnings; use strict; use Parse::RecDescent; use Data::Dumper; my $string = <<END; JOB ID=EX1J DESC='JOB1' JOB ID=EX2J DESC='JOB2' JOB ID=EX3J DESC='JOB3' JOB ID=EX4J DESC='JOB3' EX4J PRED EX2J EX3J PRED EX1J EX4J PRED EX1J END our %data; my $parser = Parse::RecDescent->new (q{ startrule : jobphrase(s) exphrase(s) jobphrase : "JOB ID=" ex "DESC=" jobquote { $::data{$item[2]}{'desc'} = $item[4]; $::data{$item[2]}{'pred'} ||=[] } exphrase : ex "PRED" ex { push @{$::data{$item[1]}{'pred'}}, @item[3] } jobquote : "'" job "'" {$return = $item[2]} ex : /EX\dJ/ job : /JOB\d/ }); $parser->startrule($string); print Dumper \%data;
Re: Best way to sort these strings ? recursive way ? use Parse::RecDescent?
by graff (Chancellor) on Oct 01, 2005 at 15:12 UTC
    And the last little bit you'll want to add to ikegami's code is simply:
    for ( sort keys %data ) { my $pred = join( ', ', @{$data{$_}{pred}} ); print "$_ desc $data{$_}{desc} PRED $pred\n"; }
    (or put "\t" in place of the multiple spaces that precede "desc" and "PRED" in the string you pass to "print".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found