By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,254 Members | 2,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,254 IT Pros & Developers. It's quick & easy.

Sort a array of substring

P: 8
Hi,
I need to order an array contains the strings order by
name server (iclsvi2 or iclsvi2 o iclsvi3)

service:ACS iclsvi2 started
service:ALM iclsvi2 started
service:BSOL iclsvi1 started
service:DBAPEX iclsvi1 started
service:DOCST iclsvi2 started

here the result I need

service:BSOL iclsvi1 started
service:DBAPEX iclsvi1 started
service:ACS iclsvi2 started
service:ALM iclsvi2 started
service:DOCST iclsvi2 started

Anyone can help me please?
Thx so mutch
Apr 4 '12 #1
Share this Question
Share on Google+
17 Replies


Rabbit
Expert Mod 10K+
P: 12,366
Grab the first 7 characters after the first space. Then you can use that to sort.

The strpos() function will find the position of the first occurrence of a substring. Use that to find the space. The substr function will return a substring from a string. Use that to pull the 7 characters after the space. After that you can sort with whatever sort algorithm you want to use.
Apr 4 '12 #2

P: 8
Thx Rabbit for the answear but I have these follow problem.

1) the log report I must order have different blank space between service:ACS and iclsvi2
2) I have a lot of name server iclsvi2, iclsvi1 but also webse02 or otrweb3 and so on

I would know how extrac name server of these different string to order.
In the Unix sheel there is a command to select a portion of string at coloumn 2 rather than column 1 or coloumn 3.
I don't know if is possible with PHP language.

Anyone can help me please?
Thx so mutch
Apr 6 '12 #3

Rabbit
Expert Mod 10K+
P: 12,366
You really have to mention that kind of stuff at the very beginning. How do you expect to get the right answer if you give us non-representative data?

Please post a new sample dataset containing the different variations contained in your data. Only then can we work towards a correct solution.
Apr 6 '12 #4

P: 8
There are a lot of log files from server.
They rappresent the status of cluster.
The log files are send in a directory and I must open order and pubblic on web.
The log files are all similar (ALIAS DB follow by CLUSTER NAME follow by STATUS) like the example I post.
I can't extract the string CLUSTER NAME because how I try to explain it's change for every log files and I can't permit to analyze all kind of logs files because are thousend!
I know only the structure of the log files.
Each rows have
ALIAS NAME [number of irregular blank space] CLUSTER NAME [number of irregular blank space] STATUS

Hope now my problem is more clear
Apr 10 '12 #5

Rabbit
Expert Mod 10K+
P: 12,366
So the number of spaces around the cluster name is unknown. But it looks like there's no spaces before or after the entire row. Use a regular expression to find the portion of the string that is bound by a space on each end. That will be your cluster name.
Apr 10 '12 #6

P: 8
Do you know what expression can I use to find portion of the string that is bound by a space on each end ? thx
Apr 11 '12 #7

Rabbit
Expert Mod 10K+
P: 12,366
This expression would do it.
Expand|Select|Wrap|Line Numbers
  1. \s(\w+)\s
Apr 11 '12 #8

P: 8
Sorry is a PHP expression???
what mean \s(\w+)\s?
Apr 12 '12 #9

Rabbit
Expert Mod 10K+
P: 12,366
It's the regular expression I mentioned in the previous post.
Apr 12 '12 #10

P: 8
is not clear.
Hope to find one solution on this form.
wrong idea
Apr 13 '12 #11

Rabbit
Expert Mod 10K+
P: 12,366
It's not clear because you don't know what regular expressions are. You should look up the documentation in php on it.
Apr 13 '12 #12

P: 8
Thanks Rabbit for your suggest.
Strange I don't thought about...
I'm not a PHP developer and I have few time to resolve these problem so I sign here to these form for a quick response from PHP developer.
If your solution is study the documentation on PHP... thank you...you solution is \s(\w+)\s ???? you think is clear for a not PHP developer?
In other response about these argument anyones post ad example or try to be clear most possibleand you say me to study the documentation?
for me was more help if you give me ad example of function to order an array or to help me to find another solution....
Apr 17 '12 #13

Rabbit
Expert Mod 10K+
P: 12,366
I don't give out code unless the requestor has shown a willingness to learn what's required. If you're working in PHP, but don't know PHP, I expect you to learn it. I'm not here to spoon feed you answers.
Apr 17 '12 #14

P: 8
anddddd is with an example you can explain, teacher ! example not solution teacher, you know the different?
Apr 18 '12 #15

P: 51
instead of the traditional from left to right character analysis..

make it from right to left??..

-because all these strings ended at the word "started"??..

ahaha.. im confused..ahaha.. :))
Apr 18 '12 #16

P: 51
\s(\w+)\s

looks familiar to me..

it is like a scape in C.
like \n \t \a..
ang maybe \s for space, \w+ for characters or word.
i'll try to use this expression..
thanks rabbit! :))
Apr 18 '12 #17

Rabbit
Expert Mod 10K+
P: 12,366
This returns the string two into the variable $matches.
Expand|Select|Wrap|Line Numbers
  1. preg_match("/\s(\w+)\s/", "one       two three", $matches);
Apr 19 '12 #18

Post your reply

Sign in to post your reply or Sign up for a free account.