Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello melissa_randel and welcome to the monastery and to the wonderful world of Perl

as a tip for your next posts i suggest to include some code you tried: you show more effort and the help can be better targeted at your level of wisdom: infact you had got good and very good replies to your question, but how many of them you understand completely?

Me too I dont understand the smart Anonymous's almost oneliner: i would need to refill it with a lot of print statements before understanding it.

Because of this i think the best approch is what the wise choroba presented you as first reply: think about your problem in words and then translate into Perl. I've started learning Perl with no programming nor scientific backgroud and after a decade of Perl i'm start thinking that the compiler is happier with plain basic code. Me too nowadays I tend to write 'smart' code but i think is often a matter of self exstimation more that a matter of quality.
So the code I present you will be easy and commented for a full understanding.
# always use stric and warnings (till the moment you know when is safe + disabling them) use strict; use warnings; # we use an array to grab DATA. array preserves order, if order in the + output is needed my @arr; # <> is something like an iterator: # $next_line = <DATA> retrieve next line # for <DATA> process all lines # we chomp all lines to remove \n at the end and then we push the @arr + with the line chomp $_ and push @arr,$_ for <DATA>; # hashes provides uniqueness of keys, and we need uniqueness because.. +. my %adj; # .. in the loop from 0 to the last index of @arr # (pay attention when using $#arr: @arr in scalar context return num o +f elements, # while $#arr is the last index of the array starting from 0 # so scalar @arr == $#arr + 1) # in the loop we process two value at time (sliding window?) checking +if the # numerical part is adjacent to the next element's numerical part for (0..$#arr){ # exit condition go EVERYTIME at the beginning of loops # so we will exit the loop if is the last element (yet processed p +reviously) last if $_ == $#arr; # grab the numerical part of interest # $1 is what inside the first matched () group. (capturing parenth +eses) my $cur_num = $1 if $arr[$_] =~/\d*[A-Z]_(\d+)$/; my $next_num = $1 if $arr[$_ + 1] =~/\d*[A-Z]_(\d+)$/; # if current is adjacent to next if ($cur_num == $next_num - 1){ # we populate the hash with nevermind values $adj{$arr[$_]} = undef; $adj{$arr[$_ + 1]} = undef; # if we had used $adj{$arr[$_]}++ (autoincrement) # you would notice the X_203 with value of 2 # because is inserted twice: as next_num while process +ing X_202 # and as current_num while X_204 } } # if the order of the data must be preserved we still have the array: # if the data was alphabetically ordered would be simpler (and the arr +ay unuseful) # simple as print "$_\n" for sor keys %adj foreach (@arr){ print "$_\n" if exists $adj{ $_ }; } __DATA__ 2L_33 2L_34 3L_45 3L_87 X_202 X_203 X_204
Obviously concise code is a good thing. But someone here at PerlMonks once said:Dont code at your best. Being to debug twice difficult then write code, you'll not be able to debug, by definition
so in the above code:
my $cur_num = $1 if $arr[$_] =~/\d*[A-Z]_(\d+)$/; my $next_num = $1 if $arr[$_ + 1] =~/\d*[A-Z]_(\d+)$/;
can be shortned (imagine a long list to process) into
my ($cur_num,$next_num) = map {$1 if $_ =~/\d*[A-Z]_(\d+)$/} $arr +[$_],$arr[$_+1];
But i suspect is not faster nor more efficient: is just more concise and uneasier to debug: the plain, kid version is the easiest to debug (because you'll get the exact line number of the statement producing the error!):
if ( $arr[$_] =~/\d*[A-Z]_(\d+)$/ ){ $cur_num = $1; }


HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Adjacent numbers - the plain way by Discipulus
in thread Adjacent numbers by melissa_randel

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 examining the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found