Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: difference in regex

by haukex (Archbishop)
on May 29, 2018 at 14:20 UTC ( [id://1215378]=note: print w/replies, xml ) Need Help??


in reply to Re^2: difference in regex
in thread difference in regex

The algo I would like to implement :
As I see it the steps are:
1. if the string has commas:
1.a. get the last comma and check if the last substring is a number - if so put it in hash like this: $hash{$path} = $value;
1.b. if the substring after the last comma isn't a number - $hash{$path} = 1;
2. if string has no commas: $hash{$string} = 1;

Although a good start, point 1.b. is unclear: in this case, do you want the whole string stored in $path, or just the part up until the last comma? For now I'm assuming the latter. Anyway, while there may always be "nicer" ways to write things in Perl (Update: and you haven't specified what you meant with "it doesn't look very good"), sometimes a good starting point is a direct translation:

use warnings; use strict; use Scalar::Util qw/looks_like_number/; use Data::Dumper; # Debug my %hash; while (my $string = <DATA>) { chomp($string); # check if string has at least one comma, and at the same # time extract the value after the last comma if ( my ($path,$value) = $string=~/^(.*),([^,]*)$/ ) { if ( looks_like_number($value) ) { $hash{$path} = $value; } else { $hash{$path} = 1; } } else { $hash{$string} = 1; } } print Dumper(\%hash); # Debug __DATA__ foo bar,x quz,5 a,b,c,42

Of course there's lots of potential for shortening that, e.g. by combining it with my example code from here. Update: A really simple shortening:

while (<DATA>) { chomp; if ( /^(.*),([^,]*)$/ ) { $hash{$1} = looks_like_number($2) ? $2 : 1 } else { $hash{$_} = 1 } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-24 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found