in reply to Re: Iterate multiple arrays with added text
in thread Iterate multiple arrays with added text

Update: I did get it to work using this code:
foreach my $i (0 .. @titles) { print "Title: $title[$i]\n"; print "Author: $author[$i]\n"; print "Date: $date[$i]\n\n"; }
However, while it outputs what I need, it gives the error:
Use of uninitialized value within @titles in concatenation (.) or stri +ng at bdb.pl line 51. Title: Use of uninitialized value within @authors in concatenation (.) or str +ing at bdb.pl line 52. Author: Use of uninitialized value within @date in concatenation (.) or string + at bdb.pl line 53. Date:
So when I'm using  print "Title: $title[$i]\n";, that $title is triggering the error.

Replies are listed 'Best First'.
Re^3: Iterate multiple arrays with added text
by stevieb (Canon) on May 23, 2017 at 20:20 UTC

    It's actually all three generating the warning.

    What you need is:

    for my $i (0 .. $#titles){ ... }

    Note the $#titles as opposed to @titles. You're essentially causing an off-by-one issue by using the array count as opposed to the element position (index). Example:

    perl -wMstrict -E 'my @x=(qw(a b c)); say "$_: $x[$_]" for (0..@x)' 0: a 1: b 2: c Use of uninitialized value in concatenation (.) or string at -e line 1 +. 3:

    vs:

    perl -wMstrict -E 'my @x=(qw(a b c)); say "$_: $x[$_]" for (0..$#x)' 0: a 1: b 2: c
Re^3: Iterate multiple arrays with added text
by 1nickt (Canon) on May 23, 2017 at 20:22 UTC

    Hi, @titles in scalar context returns the number of elements in the array. But the index of the last element is one less than that.

    To loop through the indices of your array, use $#titles instead, which will give the index of the last element.

    for my $i ( 0 .. $#titles ) { ... }

    Hope this helps!


    The way forward always starts with a minimal test.
Re^3: Iterate multiple arrays with added text
by ww (Archbishop) on May 23, 2017 at 20:34 UTC

    You've added some vars -- @thumbs and @schedpubs Deus ex machina EXCEPT that the 'god out of a machine' reference is to a theatrical/scripting device that solves the author's problem by injecting an un-foreshadowed solution (Superman -- never mentioned before -- arrives to save the damsel in distress; Dr. X -- the protagonist -- without any prior lab experience shown comes up with the magic potion to keep the sub shining; etc.) Here, the arrays certainly unforeshadowed, but lacking our psi powers (only temporarily, I assure you) we are (well, I am) at a loss for anything more helpful than a WAG.

    That WAG is that your data is inconsistent;, ie, doesn't have all the fields in each record (something you're banking on with the script you showed us [HINT, Hint: show all your relevant code, but pared down to the smallest example that illustrates your problem].

    If that's any serious chance that my WAG is on target, you'll do well to read about ways to test for the existance of data you're importing/processing and about transforming missing data to something that won't choke your program when your input (or lack thereof in this case) makes a variable "uninitialized"(the ternary operator for an off the top of my head notion).