Scott R. Godin wrote:
> Rob Dixon wrote:
> > Scott R. Godin wrote:
> > > John W. Krahn wrote:
> > > >
> > > > my %hash = do { local $/; <FH> =~ /[^\n,]+/g };
> > >
> > > but where's the implicit split of key and value there? forgive me
> > > for asking, but I just don't see it. is there some magic going on
> > > here?
> >
> > The %hash = puts /[^\n,]+/g into list context, which therefore
> > returns a list of all the matches. The regex matches all contiguous
> > strings of characters excluding comma and newline, so will
> > effectively split at both and return two fields per file record.
> > local $/, of course, enable slurp mode and reads the entire
> > file into a single string.
>
> Indeed. this construct is one I've rarely seen, which accounts for
> much of my surprise. I was already aware of 'slurp mode', but had not
> seen the localized use of do in this manner, where the filehandle was
> bound to a regex <FH> =~ /matchthis/;
>
> list context explains much here, but the usage was still surprising
> to me. I'll definitely have to remember this for future reference.


Hi Scott.

Yes it is quite neat, although I take Randal's point about it not
reflecting the paired nature of the data.

This construct is a companion to split, except that you specify
what you want instead of what you don't want. Here it's shorter,
and some may think neater, to use split:

    my %hash = do { local $/; split /[\n,]+/, <FH> };

but it's a very useful idiom for extracting, say, all numeric
strings from a line, no matter what their separator.

    my $line = '126, 255 and 714';
    my @val = $line =~ /\d+/g;
    print "$_\n" foreach @val;

output:

    126
    255
    714


A similar mechanism is the use of 'split' to return all of the
original string, including both data and separators. This can
be done by putting capturing parentheses in the regex parameter
as in:

    my $data = 'one, two,       three four five';
    my @val = split /(,)\s*/, $data;
    print "$_\n" foreach @val;

output:

    one
    ,
    two
    ,
    three four five

This is documented in perldoc -f split, but it had to be pointed
out to me a few weeks back.

Cheers,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to