Last active
November 26, 2015 10:12
-
-
Save paomian/ff959197a481e6ba7c12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::BufReader; | |
use std::io::BufRead; | |
use std::fs::File; | |
use std::path::Path; | |
use std::error::Error; | |
use std::env; | |
use std::collections::BTreeMap; | |
fn get_data(s:&str,separator:char,n:usize) -> Option<usize> { | |
let mut g = 0; | |
for (i,c) in s.chars().enumerate() { | |
if c == separator { | |
g += 1; | |
if g == n { | |
return Some(i); | |
} | |
} | |
} | |
return None; | |
} | |
fn main() { | |
let s_path = if let Some(arg) = env::args().nth(1) { | |
println!("The first argument is {}", arg); | |
arg | |
} else { | |
println!("Please input a file name"); | |
return; | |
}; | |
let s_id = if let Some(arg) = env::args().nth(2) { | |
println!("The secend argument is {}", arg); | |
arg | |
} else { | |
println!("Please input a file name"); | |
return; | |
}; | |
let path = Path::new(&s_path); | |
let p_display = path.display(); | |
let mut store_m:BTreeMap<String,usize> = BTreeMap::new(); | |
let f = match File::open(&path) { | |
Ok(v) => v, | |
Err(e) => panic!("couldn't open {}: {}", p_display, | |
Error::description(&e)), | |
}; | |
let file = BufReader::new(&f); | |
for line in file.lines() { | |
let _ = line.map(|l| { | |
if let Some(_) = l.find(&s_id) { | |
get_data(&l,' ',7).map(|x|{ | |
let tmp = &l[x+1..]; | |
get_data(tmp,' ',3).map(|y| { | |
get_data(tmp,' ',4).map(|z| { | |
let app_id = &tmp[y+1..z-1]; | |
*store_m.entry(app_id.to_string()).or_insert(0) += 1; | |
}); | |
}); | |
}); | |
} | |
}); | |
} | |
let mut v:Vec<(&str,usize)> = Vec::new(); | |
for (key, value) in store_m.iter() { | |
v.push((key,*value)); | |
} | |
v.sort_by(|&(_,a),&(_,b)| a.cmp(&b)); | |
for v in v.iter() { | |
//println!("{}:{}",key,value); | |
println!("{}:{}",v.0,v.1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment