Hatena::ブログ(Diary)

satoru.netの自由帳 このページをアンテナに追加 RSSフィード

160525Wedday

memo

#!/usr/bin/perl

use re 'eval';

my $key = "192.168.[0-100].*";
my $re = make_re($key);

print "[$re]\n";

my @lines = grep {$_} split /\n/,qq{
192.168.99.112 - - [10/Jul/2013:13:53:15 +0900] "GET /top.html HTTP/1.1" 404 1426 "http://facebook.com" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
192.168.81.20 - - [10/Jul/2013:15:06:33 +0900] "GET /hogehoge.html HTTP/1.1" 404 4374 "http://paiza.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
192.168.223.58 - - [10/Jul/2013:21:32:01 +0900] "GET /hoge.html HTTP/1.1" 304 6601 "http://paiza.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:22.0) Gecko/20100101 Firefox/22.0"
};

for(@lines){
	if(m@^$re@){
		s@\[|\]@@gim;
		my($ip,$date,$file) = (split / /)[0,3,6];
		print "$ip $date $file\n";
	}
}

sub make_re{

	my $pattern = shift;

	my @re;
	my $count = 1;

	for my $n ($pattern =~ m@([^\.]+)@g){
		if($n =~ m@\[@){ # レンジ指定
			my @n = $n =~ m@(\d+)@gim;
			push @re,sprintf '(\d+)(?(?{range($%s,%s,%s)})|(?!))',$count,$n[0],$n[1];
			$count++;
		} elsif($n eq "*") { #アスタリスク
			push @re,'\d+';
		} else { #数値
			push @re,"$n";
		}
	}
	return join('\.',@re);
}

sub range{
	my ($n,$min,$max) = @_;
	return ($n >= $min && $n <= $max)
}

160109Satday

swift / 任意のsegueに移動

performSegueWithIdentifier("hoge", sender: nil)

140211Tueday

データ保持用クラス NSUserDefaults

keyを元に呼び出せる便利クラス

アプリが終了しても保持し続けるので、前回の設定を保持したい時などに使う。


- (void) hoge {
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

 //読み出し 
 if([[defaults stringForKey:@"key"] isEqualToString:@"off"]){
     ...
 }

 //保存
 setDefault(@"key",@"value");

}

- (void)setDefault :(NSString *)key :(NSString *)value{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:value forKey:key];
    [defaults synchronize];
}



資料

http://iphone-tora.sakura.ne.jp/nsuserdefaults.html

140210Monday xcode - playSound func

xcode 音声読み上げ

音声読み上げ関数。

[self speech:string]

#include <AVFoundation/AVFoundation.h>
..

@implementation ViewController{
    AVSpeechSynthesizer *speechSynth;
}

- (IBAction)pressButton:(id)sender {
    [self speech:self.myText.text];
}

- (void) speech : (id) str{
    speechSynth = [[AVSpeechSynthesizer alloc] init];
    AVSpeechUtterance *utter = [AVSpeechUtterance speechUtteranceWithString:str];
    utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"ja-JP"];
    [speechSynth speakUtterance:utter];
    
}

関連資料:https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVSpeechUtterance_Ref/Reference/Reference.html#//apple_ref/doc/uid/TP40013449

音声ファイルを再生する方法。

[self playSound:filename]

sounds の配列に複数のファイル名を入れてランダムにどちらかの音を鳴らす。

#include <AVFoundation/AVFoundation.h>

...

@interface ViewController (){
    @public AVAudioPlayer *audio;
}

...

- (IBAction)HogePressed:(id)sender {
    NSArray *sounds = @[@"soundFile1",@"soundFile2"];
    int i = arc4random() % sounds.count;
    [self playSound:sounds[i]];
}

- (void) playSound : (id)file{
    NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:path];
    audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    audio.volume = 0.1;
    [audio play];
}

131230Monday

memo

#import <Foundation/Foundation.h>

int HOGE = 10;

int unko(int x, int y){
    NSLog(@"hikisu=[%d][%d]",x,y);
    return x*y*HOGE;
}

int main(int argc, const char * argv[])
{
 @autoreleasepool {
     NSLog(@"return=[%d]",unko(5,2));
     
 }
 return 0;
}

©satoru.net
Mail Twitter