Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 } }

In reply to Re^3: difference in regex by haukex
in thread difference in regex by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (1)
As of 2024-04-24 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found