Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello darkblackblue,

As the fellow Monks say this looks like a school assignment. Well I could suggest another approach to your problem but it only contains half of the solution, I have left the rest for you to solve.

Sample of code:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Math::Fibonacci qw( isfibonacci ); my $string = '377'; sub mySubString { my ($string, $offset, $length) = @_; return substr $string, $offset, $length; } sub fibonacciCompare { my ($s, $iteration) = @_; my @matched; for (1..length($s)) { my $inToCompare = mySubString($s, $iteration, $_); my $fibonacci = isfibonacci($inToCompare); push @matched, "Matched: $inToCompare" if ($fibonacci); } return \@matched if $matched[0]; } my $final = fibonacciCompare($string, 0); print Dumper $final if $final; __END__ $ perl test.pl $VAR1 = [ 'Matched: 3', 'Matched: 377' ];

I used different hard coded input instead of reading your data from your file but you get the point that the input is data from your file. What I am doing on this sample of code is comparing the string part by part as you wanted with a list of fibonacci numbers. The module that I am using is Math::Fibonacci. What the script does not do is to compare the numbers by subtracting the elements one by one until the end as you described. I have left this part out.

Update: I just observed that the darkblackblue has asked the question since the 28th December. I assume that he/she was not able to resolve it. Just to add complete answer to the question for future reference see sample of code bellow:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; use Math::Fibonacci qw( isfibonacci ); sub mySubString { my ($string, $offset, $length) = @_; return substr $string, $offset, $length; } sub fibonacciCompare { my ($s, $offset) = @_; my @matched; for (1..length($s)) { my $inToCompare = mySubString($s, $offset, $_); say $inToCompare; my $fibonacci = isfibonacci($inToCompare); push @matched, "Matched: $inToCompare" if ($fibonacci); } return \@matched if $matched[0]; } my %HoA; my @AoA; my $string = '377'; for (1..length($string)) { my $fibonacci = fibonacciCompare($string, 0); push @AoA, $fibonacci; $HoA{$string} = $fibonacci; $string = substr $string, 1; } print Dumper \@AoA; print Dumper \%HoA; __END__ $ perl test.pl 3 37 377 7 77 7 $VAR1 = [ [ 'Matched: 3', 'Matched: 377' ], undef, undef ]; $VAR1 = { '77' => undef, '7' => undef, '377' => [ 'Matched: 3', 'Matched: 377' ] };

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Read file text and find fibonacci series by thanos1983
in thread Read file text and find fibonacci series by darkblackblue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (4)
    As of 2019-02-21 05:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I use postfix dereferencing ...









      Results (109 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!