Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: difference in regex

by ovedpo15 (Pilgrim)
on May 29, 2018 at 13:58 UTC ( [id://1215372]=note: print w/replies, xml ) Need Help??


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

Thank you for the fast replay.
I tried to use the following regex  my($path,$value) = ($row =~ /(.*),(.*)/); to split the string.
but if there are no commas it won't work. Which regex should I use in order to always put the string into $path so I can only check if $value is defined?
for example:
if "abc" it will be $path = "abc" and $value is undefined.
if "abc,5" it will be $path = "abc" and $value = 5
if "a,b,c,5" it will be $path = "a,b,c" and $value = 5

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;


how to implement this?

Replies are listed 'Best First'.
Re^3: difference in regex
by Eily (Monsignor) on May 29, 2018 at 14:11 UTC

    Because ($path, $value) is a list, you get the list of submatches (list context). But if you do something like:

    if ($row =~ /(.*),(.*)/) { ... }
    since the if expects a boolean, the operation will return true if something matches, and false otherwise (boolean context). And you can still access the left and right part as $1 and $2. So you can do:
    my $path = $row; # path is the full string by default if ($row =~ /(.*),(.*)/) { my $left_part = $1; my $value = $2; # Check if $value is a number and change $path if needed ... }

Re^3: difference in regex
by haukex (Archbishop) on May 29, 2018 at 14:20 UTC
    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://1215372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found