sorting on regex within a record

E

Eric Smith

Hi

I have a file in the following form:

wqrrger qr grg
erg rqewg reg
_some_key_
wrgq rqweg erg
ergr rg

regre erger
_another_key_
erger ergr
fgreg
ergetr ewtghetg
ergetg wetghwetgh

rqwegerg ewet
rweqqrg ewgeg
erget ewtghet
_wherever_key_
rgerg qerg er

There is no rule as to where the key will be in the
line deliminated record.

How do I sort on the keys?
 
A

A. Sinan Unur

Hi

I have a file in the following form:

wqrrger qr grg
erg rqewg reg
_some_key_
wrgq rqweg erg
ergr rg

regre erger
_another_key_
erger ergr
fgreg
ergetr ewtghetg
ergetg wetghwetgh

rqwegerg ewet
rweqqrg ewgeg
erget ewtghet
_wherever_key_
rgerg qerg er

There is no rule as to where the key will be in the
line deliminated record.

How do you distinguish keys from other lines?

In the example above, what is the value associated with each key?

Sinan
 
W

Walter Roberson

:wqrrger qr grg
:erg rqewg reg
:_some_key_
:wrgq rqweg erg
:ergr rg

:regre erger
:_another_key_
:erger ergr
:fgreg
:ergetr ewtghetg
:ergetg wetghwetgh

:There is no rule as to where the key will be in the
:line deliminated record.

:How do I sort on the keys?

Are these multi-line records delimited by an empty line?
Is there any distinguishing feature about the key (e.g., the underscores
marking the beginning and end) ?

You might find it easiest to use a Schwartzian Transform. See e.g.
http://kw.pm.org/talks/0303-transform/TABLE_OF_CONTENTS.html
 
G

Gunnar Hjalmarsson

Eric said:
I have a file in the following form:

wqrrger qr grg
erg rqewg reg
_some_key_
wrgq rqweg erg
ergr rg

There is no rule as to where the key will be in the line
deliminated record.

How do I sort on the keys?

What have you tried? What difficulties did you encounter that made you
post here? Where is your code?

This is one idea:

my %records;
{
local $/ = '';
while (<DATA>) {
(my $key) = /_(\w+)_/;
$records{$key} = $_;
}
}
print $records{$_} for sort keys %records;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top