Professional Web Applications Themes

How to get length of string? length() problems - PERL Modules

Simplified a bit, I'm parsing HTML doents to get sentences e.g. my $html = get($URL); # remove all HTML TAGs...blah blah blah sentences = split(/\./, $html)); then I'm trying to determine the number of characters in the sentence. However, although when I print the sentences they look fine, when I use length($sentence[0]) I get values in the hundreds for small sentences. Most doentation I found said "length() returns the number of chars" however, some said "length() returns the number of bytes". To get the number of chars in this case, can I just divide by 8 or something? Thanks for ...

  1. #1

    Default How to get length of string? length() problems

    Simplified a bit, I'm parsing HTML doents to get sentences e.g.
    my $html = get($URL);
    # remove all HTML TAGs...blah blah blah
    sentences = split(/\./, $html));
    then I'm trying to determine the number of characters in the sentence.
    However, although when I print the sentences they look fine, when I use
    length($sentence[0]) I get values in the hundreds for small sentences. Most
    doentation I found said "length() returns the number of chars" however,
    some said "length() returns the number of bytes". To get the number of
    chars in this case, can I just divide by 8 or something?

    Thanks for your help.
    Mitchua


    Mitchua Guest

  2. #2

    Default Re: How to get length of string? length() problems

    "Mitchua" <mitchua> wrote in message
    news:V5XQa.53675$1aB1.35315news02.bloor.is.net.ca ble.rogers.com...
    > Simplified a bit, I'm parsing HTML doents to get sentences e.g.
    > my $html = get($URL);
    > # remove all HTML TAGs...blah blah blah
    > sentences = split(/\./, $html));
    > then I'm trying to determine the number of characters in the sentence.
    > However, although when I print the sentences they look fine, when I use
    > length($sentence[0]) I get values in the hundreds for small sentences.
    Most
    > doentation I found said "length() returns the number of chars" however,
    > some said "length() returns the number of bytes". To get the number of
    > chars in this case, can I just divide by 8 or something?
    >
    Would something like sprintf("%20s", $sentence[0]) work to crop the sentence
    to 20 characters?

    --Mitchua


    Mitchua Guest

  3. #3

    Default Re: How to get length of string? length() problems

    Mitchua wrote:
    > "Mitchua" <mitchua> wrote in message
    > news:V5XQa.53675$1aB1.35315news02.bloor.is.net.ca ble.rogers.com...
    >> Simplified a bit, I'm parsing HTML doents to get sentences e.g.
    >> my $html = get($URL);
    >> # remove all HTML TAGs...blah blah blah
    >> sentences = split(/\./, $html));
    >> then I'm trying to determine the number of characters in the sentence.
    >> However, although when I print the sentences they look fine, when I use
    >> length($sentence[0]) I get values in the hundreds for small sentences.
    > Most
    >> doentation I found said "length() returns the number of chars"
    >> however,
    >> some said "length() returns the number of bytes". To get the number of
    >> chars in this case, can I just divide by 8 or something?
    >>
    >
    > Would something like sprintf("%20s", $sentence[0]) work to crop the
    > sentence to 20 characters?
    >
    > --Mitchua
    perldoc -f length:

    "length EXPR
    length Returns the length in characters of the value of EXPR..."


    BUT length() returns the length in bytes when the bytes pragma is used, eg:

    $x = chr(400);
    print "Length is ", length $x, "\n"; # "Length is 1"
    printf "Contents are %vd\n", $x; # "Contents are 400"
    {
    use bytes;
    print "Length is ", length $x, "\n"; # "Length is 2"
    printf "Contents are %vd\n", $x; # "Contents are 198.144"
    }

    perldoc bytes for more info.

    Cheers,
    --
    Rich
    [email]scriptyrichyahoo.co.uk[/email]
    Rich Guest

  4. #4

    Default Re: How to get length of string? length() problems

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "Mitchua" <mitchua> wrote in
    news:V5XQa.53675$1aB1.35315news02.bloor.is.net.ca ble.rogers.com:
    > Simplified a bit, I'm parsing HTML doents to get sentences e.g.
    > my $html = get($URL);
    > # remove all HTML TAGs...blah blah blah
    > sentences = split(/\./, $html));
    > then I'm trying to determine the number of characters in the sentence.
    > However, although when I print the sentences they look fine, when I
    > use length($sentence[0]) I get values in the hundreds for small
    > sentences. Most doentation I found said "length() returns the
    > number of chars" however, some said "length() returns the number of
    > bytes". To get the number of chars in this case, can I just divide by
    > 8 or something?
    Only if your characters are 8 bytes wide!

    Do you have an example of input data that exhibits this length()
    discrepancy? Can you include the output of something like:

    print "[[[$string]]] ", length($string), "\n";

    - --
    Eric
    $_ = reverse sort qw p ekca lre Js reh ts
    p, $/.r, map $_.$", qw e p h tona e; print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBPxTBu2PeouIeTNHoEQIJcgCeNrC1lDNYKBtdGsL5Bw0bxd IM2BMAnRAr
    vTZutckih5KT81pj/63k5mDZ
    =1LLa
    -----END PGP SIGNATURE-----
    Eric J. Roode Guest

  5. #5

    Default Re: How to get length of string? length() problems


    "Eric J. Roode" <REMOVEsdnCAPScomcast.net> wrote in message
    news:Xns93B9EB73EF613sdn.comcast206.127.4.25...
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > "Mitchua" <mitchua> wrote in
    > news:V5XQa.53675$1aB1.35315news02.bloor.is.net.ca ble.rogers.com:
    >
    > > Simplified a bit, I'm parsing HTML doents to get sentences e.g.
    > > my $html = get($URL);
    > > # remove all HTML TAGs...blah blah blah
    > > sentences = split(/\./, $html));
    > > then I'm trying to determine the number of characters in the sentence.
    > > However, although when I print the sentences they look fine, when I
    > > use length($sentence[0]) I get values in the hundreds for small
    > > sentences. Most doentation I found said "length() returns the
    > > number of chars" however, some said "length() returns the number of
    > > bytes". To get the number of chars in this case, can I just divide by
    > > 8 or something?
    >
    > Only if your characters are 8 bytes wide!
    >
    > Do you have an example of input data that exhibits this length()
    > discrepancy?
    Checkout Rich's reply. My problem was that I was using length($sentence)
    instead of length $sentence. Once I changed that, it was all good. Thanks
    for the reply.

    Mitchua


    Mitchua Guest

  6. #6

    Default Re: How to get length of string? length() problems

    "Mitchua" <mitchua> wrote in
    news:7XkRa.89580$sI91.77734news04.bloor.is.net.ca ble.rogers.com:
    > Checkout Rich's reply. My problem was that I was using
    > length($sentence) instead of length $sentence. Once I changed that,
    > it was all good. Thanks for the reply.
    Hmmm. I fail to see how that could possibly make a difference. But hey,
    whatever works is good.

    --
    Eric
    $_ = reverse sort qw p ekca lre Js reh ts
    p, $/.r, map $_.$", qw e p h tona e; print
    Eric J. Roode Guest

Similar Threads

  1. Limit on maximum String length?
    By Heman Robinson in forum Macromedia Flex General Discussion
    Replies: 1
    Last Post: April 19th, 05:34 PM
  2. string length
    By Jeremy Watts in forum PHP Development
    Replies: 1
    Last Post: April 22nd, 07:08 AM
  3. a string length problem
    By William in forum Macromedia Director Basics
    Replies: 0
    Last Post: October 28th, 07:32 AM
  4. String length/concat problems in Ruby 1.8.0?
    By Rodman S. Regier in forum Ruby
    Replies: 1
    Last Post: October 9th, 06:14 AM
  5. V12 length string problem
    By Anek1 webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 1
    Last Post: August 26th, 03:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139