Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Making a variable from a number in front of a string

by choroba (Cardinal)
on Jul 15, 2016 at 15:54 UTC ( [id://1167842]=note: print w/replies, xml ) Need Help??


in reply to Making a variable from a number in front of a string

I used a flag variable to tell me whether I'm inside the correct section. Also, using `(...)` in a regex creates a capture group, you can add 1 to it as to any other variable. Note that you need to backslash the parentheses to give them their literal meaning.
#!/usr/bin/perl use warnings; use strict; my $printing; my $next_section = q(); while (<DATA>) { if (/^\(([0-9]+)\) .*Significant Accounting Policies/) { $printing = 1; $next_section = 1 + $1; } elsif (/^\($next_section\)\s/) { undef $printing; } print if $printing; } __DATA__ (1) Preface What is this all about. (2) Summary of Significant Accounting Policies Revenue Recognition Revenue is recognized at the time goods are sold and shipped. (3) Long-term Debt
($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,

Replies are listed 'Best First'.
Re^2: Making a variable from a number in front of a string
by porsche5k (Novice) on Jul 15, 2016 at 17:40 UTC
    Thanks for the response. Here is a complete newbie question but what goes in "(<DATA>)?"

    Is that the file location?

      The <DATA> file handle makes it possible to read the __DATA__ section at the end of the program, i.e. in this case:
      __DATA__ (1) Preface What is this all about. (2) Summary of Significant Accounting Policies Revenue Recognition Revenue is recognized at the time goods are sold and shipped. (3) Long-term Debt
      It simulates another file and it is a way to quickly test programs without having to create a separate test input file
        The DATA file handle is actually even a bit cooler than that. To set this up, Perl opens the program for read and then seeks to the first byte after the __DATA__ line. Reading from DATA means that you are actually reading the program file.

        It is possible for the program to "read itself", i.e.:

        #!/usr/bin/perl use warnings; use strict; ## DEMO of reading myself seek (DATA,0,0) or die "unable to seek $!"; print while <DATA>; __DATA__ some data would go here
        To re-read the DATA section, you could use a tell() to figure out where the initial DATA byte is and then later seek back to that initial byte offset.
      Maybe we didn't give you a crystal clear reply. First, the program should run using the data segment. Then take the data after __DATA__ and put that into "somefile". In the program, you need to add at the beginning,

      open FILE, '<', "somefile" or die "unable to open file $!";
      Now put FILE everywhere that DATA appears in the program. You will now be reading "somefile" on the disk instead of the __DATA__ section.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-25 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found