mikejones has asked for the wisdom of the Perl Monks concerning the following question:

$ perl -le 'my @val= split(/\./,"foo.bar.aaa"); print @val[0];' foo
Is there a regexp way or "better way" I only want string up to "." starting from left. thank you

Replies are listed 'Best First'.
Re: split...or better?
by zwon (Abbot) on Feb 20, 2009 at 16:47 UTC
    perl -le '"foo.bar.aaa" =~ /([^.]*)\./; print $1;'

    Upd: or just

    perl -le 'print "foo.bar.aaa" =~ /([^.]*)\./;'
Re: split...or better?
by educated_foo (Vicar) on Feb 20, 2009 at 17:24 UTC
    index is what you're looking for:
    perl -le '$x = "foo.bar.aaa"; print substr $x, 0, index $x, "."'
Re: split...or better?
by suaveant (Parson) on Feb 20, 2009 at 16:48 UTC
    Well... you can do
    my $val = (split /\./, 'foo.bar.aaa')[0]; #or my($val) = 'foo.bar.aaa' =~ /^([^.]+)/;
    The regexp method is most likely the more efficient, especially on larger strings.

                    - Ant
                    - Some of my best work - (1 2 3)

Re: split...or better?
by bruno (Friar) on Feb 20, 2009 at 16:49 UTC
    my ($matched) = "foo.bar.aaa" =~ /^(.*?)\./;
Re: split...or better?
by kennethk (Abbot) on Feb 20, 2009 at 16:55 UTC

    TIMTOWTDI. If you want to drop everything after the first ".", you could modify your split a little:

    perl -le 'my $string = "foo.bar.aaa"; ($_) = split(/\./,$string,2); print;'

    or you could use a rexexp:

    perl -le '$_ = "foo.bar.aaa"; s/\..*$//; print;'

    or

    perl -le '($_) = "foo.bar.aaa"=~ /^(.*?)\./; print;'

Re: split...or better?
by codeacrobat (Chaplain) on Feb 20, 2009 at 18:04 UTC
    For extra Timtowtdiness
    perl -le '$_="foo.bar.baz"; print /^(.*?)\./'

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});