writing get_script as an external routine callable by C

Discussion in 'Perl Misc' started by Franken Sense, May 12, 2009.

  1. With forum help, I've been able to do pretty good damage with using perl
    for input. It's *so much* easier than my alternatives with compiled
    languages that I really feel like I'm in touch with the virtue of laziness.

    I'm simply too immature with perl to, say, populate a binary tree with the
    data or do many of the things that I can do with compiled languages, once I
    have the data where I want it. This is, I think, a nice final touch by
    Mark Krahn:

    #!/usr/bin/perl
    # perl m13.pl
    use warnings;
    use strict;

    # open input file
    my $filename = 'text43.txt';
    open(my $fh, '<', $filename) or
    die "cannot open $filename for reading: $!";

    # open output file
    my $filename2 = 'outfile16.txt';
    open(my $gh, '>', $filename2) or
    die "cannot open $filename2 for writing: $!";

    local $/="";

    while ( <$fh> )
    {
    my ( $verse, @s ) = split;
    my $script = join ' ', @s;
    print $gh "$verse $script\n";
    }


    # close input and output files
    close($gh) or die("Error closing $filename2: $!");
    close($fh) or die("Error closing $filename: $!");

    # abridged output:

    44:004:002 Being grieved that they taught the people, and preached through
    Jesus the resurrection from the dead.
    44:004:003 And they laid hands on them, and put them in hold unto the next
    day: for it was now eventide.
    44:004:004 Howbeit many of them which heard the word believed; and the
    number of the men was about five thousand.
    44:004:005 And it came to pass on the morrow, that their rulers, and
    elders, and scribes,


    So now I want main to have to call a routine to get the next $verse and
    $script.

    @anything = get_script( $verse, \@s)

    sub get_script
    {

    my ( $verse, @s ) = split;
    my $script = join ' ', @s;
    return something;
    }

    Not exactly beautiful code, but my first efforts rarely look nice. The
    reference for this in the camel book is §6 : Passing References. p 224

    Then there's the matter of calling a perl subroutine from C.

    #include <EXTERN.h>
    #include <perl.h>

    static PerlInterpreter *my_perl;

    int main(int argc, char **argv, char ** env)
    {
    char *args[] = {Null};
    my_perl = perl_alloc();
    perl_construct(my_perl);
    perl_parse(my_perl, NULL, argc, argv, NULL);
    call_argv("get_script", args);
    perl_destruct(my_perl);
    perl_free(my_perl);

    return 0;
    }

    The reference here is §21 of the camel book, p. 540.

    Does any of this look close?
    --
    Frank

    Oh, What Doesn't Kill You Can Have Lingering Aftereffects!
    ~~ Al Franken,
     
    Franken Sense, May 12, 2009
    #1
    1. Advertising

  2. In Dread Ink, the Grave Hand of Ben Morrow Did Inscribe:

    > Quoth d:


    [snipped and reordered]
    >> The reference here is §21 of the camel book, p. 540.
    >>
    >> Does any of this look close?

    >
    > Reasonably so. You should be using perldoc perlembed as your reference
    > rather than the Camel book: the Camel was published shortly before the
    > release of perl 5.6.0, and a lot has changed since then. Most
    > importantly, you are missing PERL_SYS_INIT3 and PERL_SYS_TERM which must
    > be called first and last respectively.
    >
    > You should also be aware that there are a number of undocumented
    > initializations that may or may not be necessary depending on your
    > platform. You can print a C file which ought to reproduce your current
    > perl binary by running
    >
    > perl -MExtUtils::Miniperl -ewritemain
    >
    > Perl embedding is quite subtle, and I would not really recommend it
    > until you are familiar with writing XS. In order to do anything useful
    > with the embedded interpreter, you will need to know how to use the perl
    > API to get at and interpret the Perl values in the program.
    >
    > You are passing main's argc/v to perl_parse, which means your program
    > will need to be called with the same command-line arguments as perl
    > would be. If you want your C program to run a particular Perl program,
    > you will need to create your own argv array to pass to perl_parse. Note
    > that you must still pass main's argc/v/env to PERL_SYS_INIT3.
    >
    > If you really want to try this, you need to start by reading perlembed
    > and perlcall, and then probably perlapi and perlguts.


    With that amount of reading in order to embed perl into C, I'll put that on
    the reading list for my next injury. Thank you.

    > You haven't yet explained why you're trying to do this. Given your
    > inexperience with perl, it's almost certainly going to be easier to
    > stick with writing in one language at a time for now.
    >


    I've been reading §6. The semantics section does not include an example
    with inputs. I think the best example is in Tricks with Parameter Lists
    with something like:

    sub get_script {
    my($verse, $script) =@_;
    ....

    I was thinking that I would try to--thereafter--get these data in a hash,
    which is close as I'm going to able to come to getting them into a tree. I
    suppose a hash is a tree, why not?

    Anyways, below I see
    sub configuration {
    my %options = @_;
    ....

    > If you actually want to pass a ref, your sub needs to look more like
    >
    > sub get_script {
    > my ($verse, $sref) = @_;
    > my $script = join " ", @$sref;


    Ok, so a little more of the same terminology with @_ . How does one
    pronounce this symbol?

    If main were going to open my_file and get the $verse and $script that
    we've been using and calls get_script with

    getscript($verse, $script);

    and instead have:

    sub get_script {
    my %options = @_;

    , what will %options look like? Could you print the hash in alphabetic
    order? Can you search in blindingly-fast time for a particular chapter and
    verse, indicated by the obvious interpretation of $verse?
    --
    Frank

    When you encounter seemingly good advice that contradicts other seemingly
    good advice, ignore them both.
    ~~ Al Franken,
     
    Franken Sense, May 14, 2009
    #2
    1. Advertising

  3. Franken Sense <> wrote:
    >If main were going to open my_file and get the $verse and $script that
    >we've been using and calls get_script with
    >
    >getscript($verse, $script);
    >
    >and instead have:
    >
    >sub get_script {
    > my %options = @_;
    >
    >, what will %options look like?


    You are passing a list of two scalars to the sub, these are copied into
    a hash, which would mean you got a hash with one element, the value of
    $verse being the hash key and the value of $script being the hash value
    for this element.

    >Could you print the hash in alphabetic order?


    You need to be more specific. Do you want to print the keys or the
    values sorted by the alphabetical order of the keys or by the
    alpabetical order of the values? Either one is easy enough.

    >Can you search in blindingly-fast time for a particular chapter and
    >verse, indicated by the obvious interpretation of $verse?


    Can you reconstruct $verse (i.e. the hash key) from that particular
    chapter and verse? If yes, then access will be O(1) in the general case.
    You can't get any faster than that.

    If you cannot reconstruct the hash key based on chapter and verse and
    searching for that is _the_ critical operation in your program, then I
    would suggest to redesign the datastructure such that a combination of
    chapter and verse can be used as the key.

    Or use a database system instead.

    jue
     
    Jürgen Exner, May 14, 2009
    #3
  4. Ben Morrow <> wrote:
    >Quoth d:
    >> getscript($verse, $script);
    >>
    >> and instead have:
    >>
    >> sub get_script {
    >> my %options = @_;

    >
    >Umm, I don't think I'm understanding what you're doing here, or you're
    >not. In general it's best to post complete (but short) programs that you
    >have actually tested, as it makes things clearer.
    >
    >If you have
    >
    > sub get_script {
    > my %options = @_;
    >
    >then it expects to be called like
    >
    > get_script(verse => $verse, script => $script);
    >
    >and you will then end up with
    >
    > $options{verse} = $verse;
    > $options{script} = $script;
    >
    >If you call it as you have above, you will have a single key which is
    >the string value of $verse, and its corresponding value will be $script.
    >I suspect this isn't what you want.


    Actually, I think (but I may be wrong) that's exactly what he's looking
    for.

    >If you are trying to build a hash up over successive calls to
    >get_script, you will need to either declare the hash outside the sub


    Good point, missed that one.
    And if he makes %options global, then there is little point in calling
    that sub, he could just as well do a direct
    %options = ($verse -> $script);
    instead of the sub call.

    > In any case, you will want to assign the
    >provided key and value to different variables, and then insert them into
    >the hash: assigning to a hash like that replaces everything in it.


    Another good catch! So do a
    $options{$verse} = $script;
    instead of that sub call.

    jue
     
    Jürgen Exner, May 14, 2009
    #4
  5. In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:

    > Ben Morrow <> wrote:
    >>Quoth d:
    >>> getscript($verse, $script);
    >>>
    >>> and instead have:
    >>>
    >>> sub get_script {
    >>> my %options = @_;

    >>
    >>Umm, I don't think I'm understanding what you're doing here, or you're
    >>not. In general it's best to post complete (but short) programs that you
    >>have actually tested, as it makes things clearer.


    Testing now.
    >>
    >>If you have
    >>
    >> sub get_script {
    >> my %options = @_;
    >>
    >>then it expects to be called like
    >>
    >> get_script(verse => $verse, script => $script);
    >>
    >>and you will then end up with
    >>
    >> $options{verse} = $verse;
    >> $options{script} = $script;
    >>
    >>If you call it as you have above, you will have a single key which is
    >>the string value of $verse, and its corresponding value will be $script.
    >>I suspect this isn't what you want.

    >
    > Actually, I think (but I may be wrong) that's exactly what he's looking
    > for.
    >
    >>If you are trying to build a hash up over successive calls to
    >>get_script, you will need to either declare the hash outside the sub

    >
    > Good point, missed that one.
    > And if he makes %options global, then there is little point in calling
    > that sub, he could just as well do a direct
    > %options = ($verse -> $script);
    > instead of the sub call.
    >
    >> In any case, you will want to assign the
    >>provided key and value to different variables, and then insert them into
    >>the hash: assigning to a hash like that replaces everything in it.

    >
    > Another good catch! So do a
    > $options{$verse} = $script;
    > instead of that sub call.
    >
    > jue


    I'll see if I can put that all together; I think I can.

    I switched data sets to the second part of the old testament that you might
    find at gutenberg.org. I think this is a solid choice, as I didn't want to
    get to hung up on the specific form I ran into on my first sortie with
    computer exegetics, namely Acts.

    I really like the look of these new data:

    Canticle of Canticles Chapter 4


    Christ sets forth the graces of his spouse: and declares his love for
    her.

    4:1. How beautiful art thou, my love, how beautiful art thou! thy eyes
    are doves' eyes, besides what is hid within. Thy hair is as flocks of
    goats, which come up from mount Galaad.

    How beautiful art thou. . .Christ again praises the beauties of his
    church, which through the whole of this chapter are exemplified by a
    variety of metaphors, setting forth her purity, her simplicity, and her
    stability.

    ....

    Gosh, I'm gonna have to look at nuns in a different light now. Anyways,
    the form here is basically the same: if a line is a scripture, it begins
    with a number that will serve as a key for searches.

    This is the latest version:

    #!/usr/bin/perl
    # perl bb2.pl
    use warnings;
    use strict;

    # open input file
    my $filename = 'ot4.txt';
    open(my $fh, '<', $filename) or
    die "cannot open $filename for reading: $!";

    # open output file
    my $filename2 = 'outfile16.txt';
    open(my $gh, '>', $filename2) or
    die "cannot open $filename2 for writing: $!";

    local $/="";

    while ( <$fh> )
    {
    my ( $verse, @s ) = split;
    my $script = join ' ', @s;
    print "$verse $script\n";
    print $gh "$verse $script\n";
    }


    # close input and output files
    close($gh) or die("Error closing $filename2: $!");
    close($fh) or die("Error closing $filename: $!");

    # abridged output:

    C:\MinGW\source>perl bb1.pl
    Canticle of Canticles Chapter 2
    Christ caresses his spouse: he invites her to him.
    2:1. I am the flower of the field, and the lily of the valleys.
    I am the flower of the field. . .Christ professes himself the flower of
    mankind,
    yea, the Lord of all creatures: and, ver. 2, declares the excellence of
    his spo

    ....
    bite and destroy the vines.
    2:16. My beloved to me, and I to him who feedeth among the lilies,
    2:17. Till the day break, and the shadows retire. Return: be like, my
    beloved, t
    o a roe, or to a young hart upon the mountains of Bether.

    C:\MinGW\source>
    --
    Frank

    The biases the media has are much bigger than conservative or liberal.
    They're about getting ratings, about making money, about doing stories that
    are easy to cover.
    ~~ Al Franken,
     
    Franken Sense, May 14, 2009
    #5
  6. In Dread Ink, the Grave Hand of Franken Sense Did Inscribe:

    > Gosh, I'm gonna have to look at nuns in a different light now.


    http://lomas-assault.net/usenet/(!!hail mary).jpg


    > Anyways,
    > the form here is basically the same: if a line is a scripture, it begins
    > with a number that will serve as a key for searches.


    I'm bombing out here on something I think is easy. How do I populate
    @books with book1.txt that looks like

    Book of Psalms
    Book of Proverbs
    Ecclesiastes
    Solomon's Canticle of Canticles
    Book of Wisdom
    ...
    Prophecy of Zacharias
    Prophecy of Malachias
    First Book of Machabees
    Second Book of Machabees

    #!/usr/bin/perl
    # perl bb3.pl
    use warnings;
    use strict;

    # open input file
    my $filename = 'book1.txt';
    open(my $fh, '<', $filename) or
    die "cannot open $filename for reading: $!";

    # open output file
    my $filename2 = 'outfile16.txt';
    open(my $gh, '>', $filename2) or
    die "cannot open $filename2 for writing: $!";

    my @books;

    while ( <$fh> )
    {
    chomp;



    }


    # close input and output files
    close($gh) or die("Error closing $filename2: $!");
    close($fh) or die("Error closing $filename: $!");
    --
    Frank

    I said that Sean Hannity took residence up Newt Gingrich's butt from 94 to
    98. I got that from British intelligence. It turns out he only took up
    residence in 95.
    ~~ Al Franken
     
    Franken Sense, May 14, 2009
    #6
  7. Franken Sense

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    >> my $filename = 'book1.txt';
    >> open(my $fh, '<', $filename) or
    >> die "cannot open $filename for reading: $!";
    >> my @books;


    BM> chomp( my @books = <$fh> );

    BM> If you prefer, you can expand that to three statements:

    BM> my @books;
    BM> @books = <$fh>;
    BM> chomp @books;

    BM> You can also use File::Slurp::read_file, which will handle opening the
    BM> file, chomping the lines and closing the file for you.

    read_file doesn't (yet) chomp lines. that option is in the todo
    list. but it does make it easier to read in a file as lines. this should
    work fine:

    use File::Slurp ;
    chomp( my @books = read_file( $file_name ) ) ;

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, May 14, 2009
    #7
  8. In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:

    > Ben Morrow <> wrote:


    >> In any case, you will want to assign the
    >>provided key and value to different variables, and then insert them into
    >>the hash: assigning to a hash like that replaces everything in it.


    I don't quite understand this last sentence.
    >
    > Another good catch! So do a
    > $options{$verse} = $script;
    > instead of that sub call.
    >
    > jue


    Ok, so far so good. Here's what this looks like now:

    #!/usr/bin/perl
    # perl bb6.pl
    use warnings;
    use strict;

    # open input files
    my $filename = 'book1.txt';
    open(my $fh, '<', $filename) or
    die "cannot open $filename for reading: $!";

    my $filename3 = 'ot5.txt';
    open(my $hh, '<', $filename3) or
    die "cannot open $filename3 for reading: $!";

    # open output file
    my $filename2 = 'outfile16.txt';
    open(my $gh, '>', $filename2) or
    die "cannot open $filename2 for writing: $!";

    my @books;
    my %Scripts;
    my %comments;

    @books = <$fh>;
    chomp @books;
    print "@books\n";

    local $/="";

    while ( <$hh> )
    {
    my @s = split /\s+/, $_;
    my $verse = $s[0];
    my $script = join(' ', @s[1..$#s]);
    $Scripts{$verse} = $script;
    }

    print %Scripts;
    my @keys = keys %Scripts;
    print @keys;

    # close input and output files

    close($hh) or die("Error closing $filename3: $!");
    close($gh) or die("Error closing $filename2: $!");
    close($fh) or die("Error closing $filename: $!");

    Abridged output:
    Book of Psalms Book of Proverbs Ecclesiastes Solomon's Canticle of
    Canticles Book of Wisdom Ecclesiasticus Prophecy of Isaias Prophecy
    of Jeremias Lamentations of Jeremias Prophecy of Baruch Prophecy of
    ....
    My son, sow not evils in the furrows of injustice, and thou shalt not reap
    them sevenfold.Whofirst hath perfect knowledge of her. . .Christ was the
    first that had perfect knowledge of heavenly wisdom.50:8.And as the rainbow
    giving light in bright clouds, and as the flower of roses in the days of
    the spring, and as the lilies that are on the brink of the water, and as
    the sweet smelling frankincense in the time of summer.39:21.All the works
    of the Lord are exceeding good.4:11.And thou shalt be as the obedient son
    of the most High, and he will have mercy on thee more than a mother.29:25.A
    sinner that transgresseth the commandment of the Lord, shall fall into an
    ....
    49:7.16:11.It46:14.46:2.30:13.38:30.26:6.25:17.8:20.
    4:19.9:19.51:28.38:32.35:17.9:20.23:34.25:25.40:23.25:1
    ..38:6.25:12.15:1.1:26.23:13.2:5.12:19.10:4.20:19.18:1.19:
    8.49:12.14:10.21:26.3:26.Jesus3:31.10:11.34:19.34:18.21:1
    4.11:13.7:33.1:9.24:23.31:15.46:9.12:18.3:8.23:1.24:20.4:
    4.15:3.25:14.20:22.17:30.41:1.40:6.50:2.1:17.God's2:15.32:
    19.26:27.15:20.26:22.38:27.45:17.24:7.6:20.26:23.13:16.47:
    4.4:3.30:5.11:25.Wise24:32.16:24.38:21.3:30.45:20.40:4.33:


    It appears true that a hash is not ordered like a binary tree.

    Where I'm hung up now is in creating a control that separates comments from
    scriptures. Scriptures begin with numbers, comments don't. So I was
    poking around for a function like isdigit and couldn't find anything.

    If comments are to be a hash, then they need something to key on, and all
    that comes to mind is the natural numbers. Here's what the pseudosource
    looks like:

    $my $counter = 0;
    while ( <$hh> )
    {
    my @s = split /\s+/, $_;

    if (s[0] is a number)

    my $verse = $s[0];
    my $script = join(' ', @s[1..$#s]);
    $Scripts{$verse} = $script;
    else
    $counter++;
    my $comment = join(' ', @s);
    %comments($counter) = $comment;
    }

    Fishing for tips. I like Ecclesiastes and the sad jews in general like
    Janeane Garafalo, Al Franken and Jon Stewart.
    --
    Frank

    My father grew up in the Great Depression - his mother's.
    ~~ Al Franken
     
    Franken Sense, May 15, 2009
    #8
  9. In Dread Ink, the Grave Hand of Uri Guttman Did Inscribe:

    >>>>>> "BM" == Ben Morrow <> writes:

    >
    > >> my $filename = 'book1.txt';
    > >> open(my $fh, '<', $filename) or
    > >> die "cannot open $filename for reading: $!";
    > >> my @books;

    >
    > BM> chomp( my @books = <$fh> );
    >
    > BM> If you prefer, you can expand that to three statements:
    >
    > BM> my @books;
    > BM> @books = <$fh>;
    > BM> chomp @books;
    >
    > BM> You can also use File::Slurp::read_file, which will handle opening the
    > BM> file, chomping the lines and closing the file for you.
    >
    > read_file doesn't (yet) chomp lines. that option is in the todo
    > list. but it does make it easier to read in a file as lines. this should
    > work fine:
    >
    > use File::Slurp ;
    > chomp( my @books = read_file( $file_name ) ) ;
    >
    > uri



    C:\MinGW\source>perldoc File::Slurp
    No documentation found for "File::Slurp".

    C:\MinGW\source>
    --
    Frank

    ...................... o _______________ _,
    ` Good Morning! , /\_ _| | .-'_|
    `................, _\__`[_______________| _| (_|
    ] [ \, ][ ][ (_|
     
    Franken Sense, May 15, 2009
    #9
  10. Franken Sense <> wrote:
    >Where I'm hung up now is in creating a control that separates comments from
    >scriptures. Scriptures begin with numbers, comments don't. So I was
    >poking around for a function like isdigit and couldn't find anything.


    perldoc -q number:
    How do I determine whether a scalar is a number/whole/integer/float?

    jue
     
    Jürgen Exner, May 15, 2009
    #10
  11. Franken Sense

    Uri Guttman Guest

    >>>>> "FS" == Franken Sense <> writes:

    FS> C:\MinGW\source>perldoc File::Slurp
    FS> No documentation found for "File::Slurp".

    duh! you install it from cpan!

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, May 15, 2009
    #11
  12. In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:

    > Franken Sense <> wrote:
    >>Where I'm hung up now is in creating a control that separates comments from
    >>scriptures. Scriptures begin with numbers, comments don't. So I was
    >>poking around for a function like isdigit and couldn't find anything.

    >
    > perldoc -q number:
    > How do I determine whether a scalar is a number/whole/integer/float?
    >
    > jue


    How do I determine whether a scalar is a number/whole/integer/float?
    Assuming that you don't care about IEEE notations like "NaN" or
    "Infinity", you probably just want to use a regular expression.

    if (/\D/) { print "has nondigits\n" }
    if (/^\d+$/) { print "is a whole number\n" }
    if (/^-?\d+$/) { print "is an integer\n" }
    if (/^[+-]?\d+$/) { print "is a +/- integer\n" }
    if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
    if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal number\n" }
    if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
    { print "a C float\n" }

    So, I want to test whether the first byte is a digit. I think this is
    /\d/. The sad thing is that I can't even put it together at this point.

    I have @s populated in paragraph mode and then split. How does the test
    condition look with these two: s[0] /\d/ ?

    $my $counter = 0;
    while ( <$hh> )
    {
    my @s = split /\s+/, $_;

    if (s[0] is a number)

    my $verse = $s[0];
    my $script = join(' ', @s[1..$#s]);
    $Scripts{$verse} = $script;
    else
    $counter++;
    my $comment = join(' ', @s);
    %comments($counter) = $comment;
    }
    --
    Frank

    My father grew up in the Great Depression - his mother's.
    ~~ Al Franken
     
    Franken Sense, May 15, 2009
    #12
  13. Franken Sense <> wrote:

    Ok, here comes your last fish.
    I _STRONGLY_ suggest to attend a Perl class or a self-study course where
    you can learn the fundamentals of Perl in a structured and comprehensive
    way instead of digging for random bits and pieces while trying to
    implement some program.

    >In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:
    >
    >> Franken Sense <> wrote:
    >>>Where I'm hung up now is in creating a control that separates comments from
    >>>scriptures. Scriptures begin with numbers, comments don't. So I was
    >>>poking around for a function like isdigit and couldn't find anything.

    >>
    >> perldoc -q number:
    >> How do I determine whether a scalar is a number/whole/integer/float?

    >
    > How do I determine whether a scalar is a number/whole/integer/float?
    > Assuming that you don't care about IEEE notations like "NaN" or

    [...]

    Yeah, I know the answer to that FAQ, there is no need to quote
    paragraphs and paragraphs of it.

    >So, I want to test whether the first byte is a digit.


    No, you probably don't. In all likelyhood you want to check if the first
    character is a digit. Characters and bytes can be very different things.

    >I think this is
    >/\d/. The sad thing is that I can't even put it together at this point.


    That's why I strongly recommend a more structured approach to your
    learning endeavour.

    >I have @s populated in paragraph mode and then split. How does the test
    >condition look with these two: s[0] /\d/ ?


    >if (s[0] is a number)


    If you mean "begins with a digit" as you said above
    if ($s[0] =~ m/^\d/)

    If you mean "is a number" as you are saying now
    if ($s[0] =~ m/^\d+$/) #all characters are digits
    or
    if (! $s[0] =~ m/\D/) #does not contain any non-digits

    Deciphering:
    The m-operator is the match operator. It can be omitted if slashes are
    used as delimiters, therefore it doesn't show up in the FAQ. I still
    prefer to write it sometimes because it makes the program logic more
    obvious.

    Normally the m (and the s) operator will use $_. If you want them to
    operate on a different variable instead, then you need to bind that
    variable to the operation, which is achieved by the =~ operator.

    And the argument of m is a regular expression, in this case delimited by
    slashes:
    Case 1 matches if the beginning of the string is followed by a digit
    Case 2 matches if the beginning of the string is followed by 1 or more
    digits followed by the end of the string.
    Case 3 matches if the string contains (at least) one non-digit.

    For further details see "perldoc perlretut" and "perldoc perlre".

    jue
     
    Jürgen Exner, May 15, 2009
    #13
  14. Franken Sense <> wrote:
    > In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:


    >> If you mean "begins with a digit" as you said above



    So, did you mean "begins with a digit" or not?


    >> if ($s[0] =~ m/^\d/)
    >>
    >> If you mean "is a number" as you are saying now
    >> if ($s[0] =~ m/^\d+$/) #all characters are digits
    >> or
    >> if (! $s[0] =~ m/\D/) #does not contain any non-digits

    >
    > if ($s[0] =~ m/\d/) seems to work.



    That will match:

    $s[0] = 'this is not the 4th verse';

    So, did you mean "begins with a digit" or not?


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, May 15, 2009
    #14
  15. In Dread Ink, the Grave Hand of Franken Sense Did Inscribe:

    > I have @s populated in paragraph mode and then split. How does the test
    > condition look with these two: s[0] /\d/ ?


    I got that part now:

    #!/usr/bin/perl
    # perl bb7.pl
    use warnings;
    use strict;

    my $filename3 = 'ot5.txt';
    open(my $hh, '<', $filename3) or
    die "cannot open $filename3 for reading: $!";

    # open output file
    my $filename2 = 'outfile16.txt';
    open(my $gh, '>', $filename2) or
    die "cannot open $filename2 for writing: $!";

    my %Scripts;

    local $/="";

    while ( <$hh> )
    {
    my @s = split /\s+/, $_;
    my $verse = $s[0];

    if ($s[0] =~ m/\d/)
    {
    print $_;
    }
    my $script = join(' ', @s[1..$#s]);
    $Scripts{$verse} = $script;
    }


    # close input and output files

    close($hh) or die("Error closing $filename3: $!");
    close($gh) or die("Error closing $filename2: $!");

    Abridged output:

    1:1. All wisdom is from the Lord God, and hath been always with him,
    and is before all time.

    1:2. Who hath numbered the sand of the sea, and the drops of rain, and
    the days of the world? Who hath measured the height of heaven, and the
    breadth of the earth, and the depth of the abyss?

    1:3. Who hath searched out the wisdom of God that goeth before all
    things?
    ....
    26:28. Two sorts of callings have appeared to me hard and dangerous: a
    merchant is hardly free from negligence: and a huckster shall not be
    justified from the sins of the lips.

    27:1. Through poverty many have sinned: and he that seeketh to be
    enriched, turneth away his eye.
    ....

    Thanks for your help.
    --
    Frank

    [Newt Gingrich] is the most unpopular politician in America. His favorable
    rating is only four points higher than the Unabomber.
    ~~ Al Franken, 1996
     
    Franken Sense, May 15, 2009
    #15
  16. In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:

    > Franken Sense <> wrote:
    >
    > Ok, here comes your last fish.
    > I _STRONGLY_ suggest to attend a Perl class or a self-study course where
    > you can learn the fundamentals of Perl in a structured and comprehensive
    > way instead of digging for random bits and pieces while trying to
    > implement some program.
    >
    >>In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:
    >>
    >>> Franken Sense <> wrote:
    >>>>Where I'm hung up now is in creating a control that separates comments from
    >>>>scriptures. Scriptures begin with numbers, comments don't. So I was
    >>>>poking around for a function like isdigit and couldn't find anything.
    >>>
    >>> perldoc -q number:
    >>> How do I determine whether a scalar is a number/whole/integer/float?

    >>
    >> How do I determine whether a scalar is a number/whole/integer/float?
    >> Assuming that you don't care about IEEE notations like "NaN" or

    > [...]
    >
    > Yeah, I know the answer to that FAQ, there is no need to quote
    > paragraphs and paragraphs of it.
    >
    >>So, I want to test whether the first byte is a digit.

    >
    > No, you probably don't. In all likelyhood you want to check if the first
    > character is a digit. Characters and bytes can be very different things.
    >
    >>I think this is
    >>/\d/. The sad thing is that I can't even put it together at this point.

    >
    > That's why I strongly recommend a more structured approach to your
    > learning endeavour.
    >
    >>I have @s populated in paragraph mode and then split. How does the test
    >>condition look with these two: s[0] /\d/ ?

    >
    >>if (s[0] is a number)

    >
    > If you mean "begins with a digit" as you said above
    > if ($s[0] =~ m/^\d/)
    >
    > If you mean "is a number" as you are saying now
    > if ($s[0] =~ m/^\d+$/) #all characters are digits
    > or
    > if (! $s[0] =~ m/\D/) #does not contain any non-digits


    if ($s[0] =~ m/\d/) seems to work.

    >
    > Deciphering:
    > The m-operator is the match operator. It can be omitted if slashes are
    > used as delimiters, therefore it doesn't show up in the FAQ. I still
    > prefer to write it sometimes because it makes the program logic more
    > obvious.
    >
    > Normally the m (and the s) operator will use $_. If you want them to
    > operate on a different variable instead, then you need to bind that
    > variable to the operation, which is achieved by the =~ operator.
    >
    > And the argument of m is a regular expression, in this case delimited by
    > slashes:
    > Case 1 matches if the beginning of the string is followed by a digit
    > Case 2 matches if the beginning of the string is followed by 1 or more
    > digits followed by the end of the string.
    > Case 3 matches if the string contains (at least) one non-digit.
    >
    > For further details see "perldoc perlretut" and "perldoc perlre".
    >
    > jue


    Thx. I'll take a look later.
    --
    Frank

    If you put the two Bushs together in their over seven years of their two
    presidencies, not one new job has been created. Numbers do not lie. If you
    extrapolated from that, if the Bushs had run this country from its very
    beginning to the current time, not one American would have ever worked.
    We'd be hunter-gatherers.
    ~~ Al Franken, in response to the 2004 SOTU address
     
    Franken Sense, May 15, 2009
    #16
  17. Franken Sense <> wrote:

    > It appears true that a hash is not ordered



    What do you mean "appears"?

    The documentation for the data structure you mention says
    quite clearly that it is not ordered.

    perldoc perldata

    ... Hashes are unordered...


    > If comments are to be a hash, then they need something to key on, and all
    > that comes to mind is the natural numbers.



    The data structure that is indexed by numbers is called an "array".

    A hash data structure is indexed by strings.


    > Fishing for tips.



    No need to state the painfully obvious.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, May 15, 2009
    #17
  18. Franken Sense

    Uri Guttman Guest

    >>>>> "FS" == Franken Sense <> writes:

    FS> if ($s[0] =~ m/\d/) seems to work.

    seems to work is not a proper logical statement. it works or it
    doesn't. does it work for all test cases? for a non-number? for a number
    with garbage in front of it? you have to think a bit before you claim
    something works and you don't even understand why it may fail.

    and i agree. take a perl class. read a whole perl book and do the
    exercises. this dribs and drabs approach is not the way to learn a
    programming language. it is like learning to drive but pushing all the
    pedals, levers and buttons in any order with random pressures and seeing
    what may or may not happen.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, May 15, 2009
    #18
  19. Franken Sense <> wrote:
    >In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:
    >> Franken Sense <> wrote:
    >>>So, I want to test whether the first byte is a digit.

    >>
    >> No, you probably don't. In all likelyhood you want to check if the first
    >> character is a digit. Characters and bytes can be very different things.


    >> If you mean "begins with a digit" as you said above
    >> if ($s[0] =~ m/^\d/)
    >>
    >> If you mean "is a number" as you are saying now
    >> if ($s[0] =~ m/^\d+$/) #all characters are digits
    >> or
    >> if (! $s[0] =~ m/\D/) #does not contain any non-digits

    >
    > if ($s[0] =~ m/\d/) seems to work.


    Between "works" and "seems to work" is a similar difference as between
    "well done" and "meant well".

    jue
     
    Jürgen Exner, May 15, 2009
    #19
  20. In Dread Ink, the Grave Hand of Uri Guttman Did Inscribe:

    >>>>>> "FS" == Franken Sense <> writes:

    >
    > FS> C:\MinGW\source>perldoc File::Slurp
    > FS> No documentation found for "File::Slurp".
    >
    > duh! you install it from cpan!
    >
    > uri


    I'm curious how you treat your clients. I was looking for documentation.

    Do you tell them to go back to college? lose weight?

    As OP, I certainly didn't ask about the dumb sidebar issue of File::Slurp.
    --
    Frank

    I said that Sean Hannity took residence up Newt Gingrich's butt from 94 to
    98. I got that from British intelligence. It turns out he only took up
    residence in 95.
    ~~ Al Franken
     
    Franken Sense, May 17, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. exiquio
    Replies:
    2
    Views:
    565
    exiquio
    Oct 7, 2008
  2. Franken Sense

    writing get_script()

    Franken Sense, May 7, 2009, in forum: C Programming
    Replies:
    29
    Views:
    1,003
    Barry Schwarz
    May 27, 2009
  3. Bazil
    Replies:
    2
    Views:
    126
    Tad McClellan
    Dec 6, 2003
  4. Frank Seitz

    writing get_script()

    Frank Seitz, May 7, 2009, in forum: Perl Misc
    Replies:
    38
    Views:
    323
    Franken Sense
    May 12, 2009
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    141
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page