スポンサーリンク

Perlで文字列を/で分割(split)しよう

久しぶりにファイルパスの文字列を分解しようとしたのですが、書き方をスコーン! と忘れてしまっていたので自分用にメモメモです φ(..)。

単純に区切り文字になる / を \ でエスケープしてやれば良いだけの話なんですが、何で抜けてたかなぁ?(^_^;)

#!/usr/bin/perl

use strict;
use warnings;

my $pathStr = '/home/mukairiku/hoge/sample.txt';
my @pathArray  = split(/\//, $pathStr);

print shift(@pathArray)."\n"; # 結果は ""(空の文字列)
print pop(@pathArray)."\n";   # 結果は "sample.txt"
 

うん、これでOK!

スポンサーリンク
コメント
Verify your Comment
Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

コメントを投稿
トラックバック

このページのトラックバックURL:
https://www.typepad.com/services/trackback/6a01310f1f31c0970c01b7c7733739970b

このページへのトラックバック一覧 Perlで文字列を/で分割(split)しよう:


プロフィール


  • 書いてる人:夢界 陸

    名古屋在住のおっさん。
    プログラミングやガジェットの話など、 日々の興味を徒然と綴っています。



    Twitterやってます @mukairiku

    運営サイト
    www.mukairiku.net

ブログ内検索

Licenses

  • Creative Commons License

OTHER

  • このブログのはてなブックマーク数

Blog powered by TypePad

スポンサーリンク