开发者

How to give a string as input to cut command in perl

I have a 开发者_如何学编程string say my $str = "Hello Hi Bye", and I want supply this string to cut command get the fields. Can anyone please help me how to do this?

And what about this? I was actually trying to do like this...I have an array:

my @str = ("hello hi bye","hello hi you","abc def ghi","abc def jkl");

I want to provide this array to cut the first two fields and get the unique one among them. Like output should be "hello hi" and "abc def" and get the count of such unique combination of those 2 fields, here it is 2..


split

use warnings;
use strict;

my $str = "Hello Hi Bye";
my @fields = split /\s+/, $str;

Whenever you want "unique", think "hash":

use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;

my %count;
my @str = ("hello hi bye","hello hi you","abc def ghi","abc def jkl"); 
for (@str) {
    my @flds = (split)[0..1];
    $count{"@flds"}++;
}
print Dumper(\%count);

__END__

$VAR1 = {
          'abc def' => 2,
          'hello hi' => 2
        };


Try using the split function:

my $str = "Hello Hi Bye";
my @fields = split(/\s+/, $str); # => ('Hello', 'Hi', 'Bye')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜