Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Case insensitive string comparison (updated x2)

by Marshall (Canon)
on Jun 28, 2020 at 01:40 UTC ( [id://11118614]=note: print w/replies, xml ) Need Help??


in reply to Re: Case insensitive string comparison (updated x2)
in thread Case insensitive string comparison

I was also confused about what this $$blk_ref was about. And I just punted that issue in my direct post. It would be helpful if the OP showed more of his application. Dereferencing a ref to a single scalar is a relatively rare thing in Perl. That is because Perl array iterator operations are very good at hiding this nastiness.

For fun, I used your example data and coded the loop a couple of different ways. Neither of which use an explicit dereferencing operation.

use strict; use warnings; use Data::Dump qw(dd); my @strings = ( 'SMS,SMS1,20190811', 'SMS,SMSh,20190811', 'SMS,SMSH,20190811', 'SMS,SMSx,20190811', 'SMS,SMSi,20190811', 'SMS,SMSX,20190811', 'SMS,SMSI,20190811', ); # map{} is a logical thought for an array transformation # # Could assign back to @strings or can make # a new array, @strings2 # Could use an "if" and concatenate a message if true # and return $_ in any event. Ternary operator here # gives a place to put a single token that is # not "SMSblk" my @strings2 = map{/SMS[1HI]/i ? "$_ is SMSblk":$_}@strings; dd \@strings2; =prints: [ "SMS,SMS1,20190811 is SMSblk", "SMS,SMSh,20190811 is SMSblk", "SMS,SMSH,20190811 is SMSblk", "SMS,SMSx,20190811", "SMS,SMSi,20190811 is SMSblk", "SMS,SMSX,20190811", "SMS,SMSI,20190811 is SMSblk", ] =cut # Or with a for loop instead of map{} # to modify original array: # Foreach creates an alias and modifying that # alias modifies the original array # No tricky dereferencing is needed. foreach (@strings) { $_ .= " is SMSblk" if /SMS[1HI]/i; } dd \@strings; =prints: [ "SMS,SMS1,20190811 is SMSblk", "SMS,SMSh,20190811 is SMSblk", "SMS,SMSH,20190811 is SMSblk", "SMS,SMSx,20190811", "SMS,SMSi,20190811 is SMSblk", "SMS,SMSX,20190811", "SMS,SMSI,20190811 is SMSblk", ] =cut

Replies are listed 'Best First'.
Re^3: Case insensitive string comparison (updated x2)
by AnomalousMonk (Archbishop) on Jun 28, 2020 at 03:16 UTC
    ... a ref to a single scalar ...

    My guess about a possible rationale for this is that the strings being handled are in reality very long and DAN0207 wants to avoid making a bunch of copies of these long strings (e.g., to pass to subroutines). In a case like this, I'd agree that taking a reference to a scalar (string) and passing it around could be quite advantageous in the right circumstances. But this is all just guesswork.


    Give a man a fish:  <%-{-{-{-<

      My guess is that the OP has an array of similar lines, not just a ref to a single line. If he wants to send that array info to a sub, then pass a reference to that array. The OP provided nothing in terms of work flow and we are left to guess. With the info given, passing a ref to a single line doesn't appear to make any sense. Passing a ref to the entire array of strings would make sense.

      I also don't really understand why some csv lines should be transformed into a single token and others left as they are. This transformation is presumably being done for some future processing. Ok, what processing is that? And why is condensing a subset of lines to a particular single token result necessary?

      Maybe what is of interest are the lines that don't match the pattern? Maybe the returned array should be limited to the lines that don't match? I have no idea because this is application specific.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 20:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found