Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: $1, etc. are not just strings

by gam3 (Curate)
on Jan 04, 2007 at 16:36 UTC ( #592980=note: print w/ replies, xml ) Need Help??

in reply to $1, etc. are not just strings

I like to use this syntax to capture from regular expresions.

if (my ($a, $b) = ("$key$val" =~ /(..)(...)/)) { $hash2{$a} = $b; }
#!/usr/bin/perl use Test::More "no_plan"; use Devel::Size "total_size"; my $key = "aa"; my $val = "a00"; my %hash1; my %hash2; while (length($key) == 2) { $hash1{$key} = $val; if (my ($a, $b) = ("$key$val" =~ /(..)(...)/)) { $hash2{$a} = $b; } ++$key; ++$val; } is(keys(%hash1), keys(%hash2), "same number of keys"); is_deeply(\%hash1, \%hash2, "is_deeply same"); is(total_size(\%hash1), total_size(\%hash2)); __END__ ok 1 - same number of keys ok 2 - is_deeply same ok 3
-- gam3
A picture is worth a thousand words, but takes 200K.

Comment on Re: $1, etc. are not just strings
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://592980]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2015-11-26 21:09 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (707 votes), past polls