Sort Part of a string

D

Dylan Reinhold

Ok i have a list of file names but I only want to sort by part of the
file name. Im not sure where to start. I can only find ways to sort the
whole strings.

@files('list_20040714.log', 'list_20040715.log', 'all_20040715.log',
'all_20040714.log');

I want to sort by the date only ( newest date first).
So output would look something like

all_20040715.log
list_20040715.log
all_20040714.log
list_20040714.log

Any pointers would be very helpful.

Thanks,
Dylan
 
E

Eugene Mikheyev

@files('list_20040714.log', 'list_20040715.log', 'all_20040715.log',
'all_20040714.log');

I want to sort by the date only ( newest date first).
So output would look something like

all_20040715.log
list_20040715.log
all_20040714.log
list_20040714.log

sort {
my($a_date) = $a =~ /(\d+)\.log/o;
my($b_date) = $b =~ /(\d+)\.log/o;
$a <=> $b
} @files;
 
D

Dylan Reinhold

Eugene said:
sort {
my($a_date) = $a =~ /(\d+)\.log/o;
my($b_date) = $b =~ /(\d+)\.log/o;
$a <=> $b
} @files;

Thank you so much, I did have to make some changes to get it to work.


sort {
my($a_date) = $a =~ /(\d+)\.log/o;
my($b_date) = $b =~ /(\d+)\.log/o;
$b_date <=> $a_date # had to change from $a to $a_date and b.
} @files;
 
E

Eugene Mikheyev

sort {
Thank you so much, I did have to make some changes to get it to work.
sort {
my($a_date) = $a =~ /(\d+)\.log/o;
my($b_date) = $b =~ /(\d+)\.log/o;
$b_date <=> $a_date # had to change from $a to $a_date and b.
} @files;

I assumed ascending order. If you want descending, swap vars, of course
 
E

Eugene Mikheyev

I know what it does.
So why asking?
Why do you think you need it there?
I need it in every piece of code that contains a constant regex compiled
multiple times.
 
E

Eugene Mikheyev

I think you misunderstand the /o option.
I may think many thoughts of your misunderstandings. Keep your thoughts with
you. I didn't ask for your advices
 
A

Anno Siegel

Eugene Mikheyev said:
I may think many thoughts of your misunderstandings. Keep your thoughts with
you. I didn't ask for your advices

Then don't post code.

The code you showed had the /o modifier (twice) where it isn't needed
and does nothing. Don't expect that to go uncommented.

Anno
 
E

Eugene Mikheyev

The code you showed had the /o modifier (twice) where it isn't needed
and does nothing. Don't expect that to go uncommented.
Ok. Let it do nothing. The sense is - a guy asks question, gets an answer.
He can do whatever he wants with the answer, and he does. And then a hundred
of others come out of somewhere and point to some places in the code and
scratch their asses. That's a shame for you to join this thread, it should
have become dead already. Sometimes some of the guys and gals here are too
pedantic. Far too pedantic.
 
A

Anno Siegel

Eugene Mikheyev said:
Ok. Let it do nothing.

The fact that you used it shows that you don't know what it does.
That makes readers distrust your code in other ways. You should be
grateful for a tip that can make your future code more trustworthy.
The sense is - a guy asks question, gets an answer.
He can do whatever he wants with the answer, and he does. And then a hundred
of others come out of somewhere and point to some places in the code and
scratch their asses. That's a shame for you to join this thread, it should
have become dead already.

Not for you to decide.
Sometimes some of the guys and gals here are too
pedantic. Far too pedantic.

If that's what you think, you won't be happy on clpm. Better leave now
than later.

Anno
 
E

Eugene Mikheyev

The fact that you used it shows that you don't know what it does.
That makes readers distrust your code in other ways. You should be
grateful for a tip that can make your future code more trustworthy.
That thing is not harmful at all. What is trustworthy than? You sound like a
stupid schoolboy ar schoolgirl who tries to compete with everybody. You're
not the one in the world. Just try to get into the community. I can
confess - yes, my fault, that pretty thing is really unnecessary in the
case. But does it matter? No. I assume the guy can decide how to write it
himself. I assume he does know something. And you assume he's ignorant at
all. So who is wiser? You're playing a guru. You won't go far with that. Do
you answer questions here, or just scratching your ass and asking yourself -
why did they post such an ugly code - I can write better. I can write the
best!
Anyway, I have never asked your advices. And anybody else, too.
Not for you to decide.
If that's what you think, you won't be happy on clpm. Better leave now
than later.
Not for you to decide.
 
J

Jürgen Exner

Eugene said:
Ok. Let it do nothing. The sense is - a guy asks question, gets an
answer. He can do whatever he wants with the answer, and he does. And
then a hundred of others come out of somewhere and point to some
places in the code and scratch their asses. That's a shame for you to
join this thread, it should have become dead already. Sometimes some
of the guys and gals here are too pedantic. Far too pedantic.

What the ....? If you publish code other people will look at it, why would
you post it otherwise? They want to see how others are approaching the
problem. If they find a code part that appears to be unclear, then they are
not allowed to ask why the author did it that way? There could be two
reasons:
- it was on purpose and it was a good way to do it, but the reader didn't
know about it. Cool, the reader learned something new.
- it was not a good idea. Cool, then the author learned something new.

Now, if discussing about Perl code in a newsgroup named CLPM is not
appropriate then I am sure you can explain what the purpose of this NG is.

jue
 
E

Eugene Mikheyev

You're right. I'm on your side.
But I will never be on the side of a man saying: "Man, you're stupid. This
thing is meaningless".
The guy should have said: "Man, I thought this thing is useless here. Why
did you use it?". - And then the author learns something new. This is the
right way.
 
A

Anno Siegel

Eugene Mikheyev said:
You're right. I'm on your side.
But I will never be on the side of a man saying: "Man, you're stupid. This
thing is meaningless".
The guy should have said: "Man, I thought this thing is useless here. Why
did you use it?". - And then the author learns something new. This is the
right way.

....and this is what happened (quoted from Bernhard's reply to you):
sort {
my($a_date) = $a =~ /(\d+)\.log/o;
my($b_date) = $b =~ /(\d+)\.log/o;
^^^

Why the /o?

How is that not the right way?

Anno
 
E

Eugene Mikheyev

Ok, maybe I'm tired and see non-existent things. Then I should apologize.
Should I?
 
T

Tore Aursand

I may think many thoughts of your misunderstandings. Keep your thoughts
with you. I didn't ask for your advices

But the OP ask for an advice, and it's proper behaving to give him the
best advice possible. You didn't, as others have pointed out.

So. Excactly _why_ did you use that /o option? My guess is that you
either don't know what it does (have read/seen it being used elsewhere),
or that misunderstand it's use.

Why so hard feelings when someone gives _you_ an advice?
 
A

Anno Siegel

That thing is not harmful at all. What is trustworthy than? You sound like a
stupid schoolboy ar schoolgirl who tries to compete with everybody. You're
not the one in the world. Just try to get into the community. I can
confess - yes, my fault, that pretty thing is really unnecessary in the
case. But does it matter? No.

I told you why it matters. People who see your code will not believe
you are a competent Perl coder.
I assume the guy can decide how to write it
himself. I assume he does know something. And you assume he's ignorant at
all. So who is wiser? You're playing a guru. You won't go far with that. Do
you answer questions here,

Yes. Thousands, over the years.
or just scratching your ass and asking yourself -

That's the second time you call me an ass-scratcher. No-one has insulted
you in this thread so far. Who is the intolerant one here?
why did they post such an ugly code - I can write better. I can write the
best!
Anyway, I have never asked your advices. And anybody else, too.

Well, that's usenet for you. If you post, expect comments.
Not for you to decide.

Just a bit of friendly advice.

Anno
 
T

Tom Regner

Eugene said:
That thing is not harmful at all. What is trustworthy than? You sound like
a stupid schoolboy ar schoolgirl who tries to compete with everybody.
You're not the one in the world. Just try to get into the community. I can
confess - yes, my fault, that pretty thing is really unnecessary in the
case. But does it matter?
Yes, it does:

If you post code you don't understand, your code can't be trusted -- harmful
or not is /not/ the question; giving solutions that do the trick, but
contain harmful portions (and a "does nothing block" is harmful, consider
yourself to maintain code full of such burdens) is a threat to the
community as a whole that has to be corrected, because someday someone uses
this tread to learn something new, and code like yours will spoil these
aspiring coders.

I assume the guy can decide how to write it himself.
Maybe, maybe not; if you can't tell what a piece of your code does, how can
you expect the one you are giving advice to surpass you in that
understanding?
why did they post such an ugly code - I can write better. I can
write the best!
Anyway, I have never asked your advices. And anybody else, too.
Yes you have, posting code in a public place is implicitly asking for advice
-- you could have mailed the OP if you feared comments.
Not for you to decide.
Frankly I don't care, as you are killfiled as of this moment.

hand
Tom Regner
 
E

Eugene Mikheyev

I've already confessed on that :) Yes, my fault. And guys, I shall never
use that /o option with constant regexes, I promise!
 
E

Eugene Mikheyev

Let's play another game from here :)

my $re = qr{(\d+)\.log$};
....
sort {
my($a_part) = $a =~ $re;
my($b_part) = $b =~ $re;
$b_part <=> $a_part
}

:)
 

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
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top