Perl - cannot use string (...) as ref array

I am practicing Perl with a call to codeeval.com and I am getting an unexpected error. The goal is to iterate sequentially through the file line by line, in which each line has a line and a comma separated character, and to find the most correct occurrence of that character in the line. I got the wrong answers, so I changed the code to print only the variable values ​​when I got the following error:

Can't use string ("Hello world") as an ARRAY ref while "strict refs" in use at char_pos.pl line 20, <FILE> line 1.

My code is below. You can see a sample from the file in the header. You can also see the original output that was incorrectly displaying only the rightmost character on each line.

#CodeEval challenge: https://www.codeeval.com/open_challenges/31/
#Call with $> char_pos.pl numbers
##Hello world, d
##Hola mundo, H
##Keyboard, b
##Connecticut, n

#!/usr/bin/perl
use strict;
use warnings;

my $path = $ARGV[0];

open FILE, $path or die $!;

my $len;

while(<FILE>)
{
    my @args = split(/,/,$_);
    $len = length($args[0]) - 1;
    print "$len\n";
    for(;$len >= 0; $len--)
    {
        last if $args[0][$len] == $args[1];
    }


    #if($len > -1)
    #{
    # print $len, "\n";   
    #}else
    #{
    # print "not found\n";   
    #}

}

      

EDIT: Based on the answers below, here is the code I got:

#!/usr/bin/perl
use strict;
use warnings;
use autodie;


open my $fh,"<",shift;

while(my $line = <$fh>)
{
    chomp $line;
    my @args = split(/,/,$line);
    my $index = rindex($args[0],$args[1]);

    print $index>-1 ? "$index\n" : "Not found\n";
}

close $fh;

      

+3


source to share


3 answers


It looks like you need to learn a little about Perl features. Perl has many functions for strings and scalars, and it is not always possible to know them all at once.

However, Perl has an excellent rindex feature that does exactly what you want. You give it a string, a substring (in this case, one character), and it looks for the first position of that substring on the right side of the string ( index does the same on the left.)

Since you're learning Perl, it might be a good idea to get some books on Modern Perl and standard coding guidelines. Thus, you know new coding methods and standard coding methods.

Here's a sample program:



#!/usr/bin/perl

use strict;
use warnings;
use autodie;
use feature qw(say);

open my $fh, "<", shift;

while ( my $line = <$fh> ) {
    chomp $line;
    my ($string, $char) = split /,/, $line, 2;
    if ( length $char != 1 or not defined $string ) {
        say qq(Invalid line "$line".);
        next;
    }
    my $location = rindex $string, $char;
    if ( $location != -1 ) {
        say qq(The right most "$char" is at position $location in "$string".);
    }
    else {
        say qq(The character "$char" wasn't found in line "$line".)";
}
close $fh;

      

A few suggestions:

  • use autodie

    allows your program to automatically die on bad open

    . No need to check.
  • Three parameters are open

    now deprecated.
  • Use scalar variables for file descriptors. They are easier to pass to subroutines.
  • Use lexically copied variables for loops. Try to avoid using $_

    .
  • Always read chomp

    after reading.

And most importantly, error checking ! I check the format of the string to make sure there is only one comma and that the character I'm looking for is a character. I also check the exit value rindex

to make sure it found the symbol. If it rindex

doesn't find a character, it returns -1

.

Also be aware that the first character in the string 0

, not 1

. You may have to adjust to this depending on what kind of result you expect.

+3


source


Perl strings are a base type, not indexed arrays. You would use a function substr

to get single characters (which are also strings) or substrings from them.



Also note that string comparisons are performed using eq

; ==

- numerical comparison.

+3


source


while($i=<DATA>){
($string,$char)=split(",",$i);
push(@str,$string);}
@join=split("",$_), print "$join[-1]\n",foreach(@str);



__DATA__
Hello world, d
Hola mundo, H
Keyboard, b
Connecticut, n

      

0


source







All Articles