Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Split and print hash based on regex

by Maire (Scribe)
on Mar 27, 2018 at 15:42 UTC ( [id://1211858]=note: print w/replies, xml ) Need Help??


in reply to Re: Split and print hash based on regex
in thread Split and print hash based on regex

This is a very good question, thanks! And I'm guessing from your response that this may lie at the heart of the problem? The original script that I am working with (reproduced now in an edit to my original post) used very similar syntax successfully, but I need to think about how the original script manages to populate $_ and my modified script doesn't.
  • Comment on Re^2: Split and print hash based on regex

Replies are listed 'Best First'.
Re^3: Split and print hash based on regex
by choroba (Cardinal) on Mar 28, 2018 at 03:43 UTC
    while (<DATA>)

    is equivalent to

    while ($_ = <DATA>)

    which is interpreted as

    while (defined($_ = <DATA>))

    So that's how $_ is populated in the original script.

    There's another question, though: How $1 is populated. Note that the matching uses =, not =~, so it's equivalent to

    my($regex) = ($_ =~ /This is/g)
    where the parentheses after my enforce the list context on the match, but without a capture group in the regex, there's no way to populate $1.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Ah okay, that makes a lot more sense now. Thank you for your very clear explanation: it's very much appreciated.

        choroba is not wrong to write here that "... without a capture group in the regex, there's no way to populate $1." But as always with regexes, it's more complicated:

        c:\@Work\Perl\monks>perl -wMstrict -le "$_ = 'This is not an exit'; ;; if (my ($regex) = /This is/g) { print qq{matched, captured '$regex'}; } " matched, captured 'This is'
        Due to the influence of the  /g modifier, something is captured, it's just not anything useful | useful in this case. The only purpose the capture serves in the second OPed SSCCE (the one added in an update) would be to produce a perplexing error message if the open operation failed in the
            open ... or die "could not open 'UserA$regex.txt' $!";
        statement. See Matching in list context in perlop.


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1211858]
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-23 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found