0

I want to search for a pattern inside files and compare the lines containing the same pattern.Firstly, I want to split the lines based on space and store them in an array and then compare. Depending on the size array @store, the multidimensional array (@list_of_arguments)should get updated. And the count variable is for the number of files in which the pattern is present.

expected output-->the lines containing the search pattern must split up based on spaces and the elements of all such arrays must sit in different multidimensional array locations for further operation.

This gives a compilation error in the syntax of the multidimensional array. Below is the small code snippet.

if ($line =~ /$string/)
{
print "$line\n";
  @store = split(" ",$line);
  my $size = @store;
  print "$size\n"; 
  for($l=0;$l<$size;$l++){
     @list_of_arguments[$count][$size] = @store[$size];
     print "@list_of_arguments\n";
}
}
5
  • 1
    Please update your question to include a) a minimal (running) script to display the error you get, b) the complete error message, c) your input and d) your desired output. Commented Nov 28, 2019 at 12:12
  • 1
    And please format your source code to make it better readable. Commented Nov 28, 2019 at 12:26
  • updated the question. Hope now its more clear. Commented Dec 1, 2019 at 16:52
  • I suggest to work through a tutorial on "references" perlreftut, since you must understand them in order to work with complex data structures like multidimensional arrays. Then read the data-structure cookbook, perldsc Commented Dec 3, 2019 at 18:45
  • The thing is, an array element can only be a "scalar", a single value; so if you want to store an array in a slot of another array, you need to store its "reference" -- which is a single value which has that array's memory address, so it allows us to get to the whole array. So put that in one slot, then a reference to another array goes into the other slot of the main array, etc; then you end up having an array which stores arrays. But we can't explain these basics well in a stackoverflow post or comment, thus my suggestion to read about it. Commented Dec 3, 2019 at 18:51

1 Answer 1

1

In Perl, arrays are not of fixed length and there are no multidimensional arrays either. You can have Arrays that contain (references to) other arrays.

So in your case, you can get rid of the loop and simply say

$list_of_arguments[$count] = \@store;

Edit: Adressing the comment below, this is what you want methinks:

my @list_of_arguments;

while ( my $line = <FILE> )
{
  if ($line =~ /$string/)
  {
    my @line_data = split(" ",$line);
    push @list_of_arguments, \@line_data;
  }
}

The variables @list_of_arguments and $string are poorly named though. We should change them to something more descriptive. We also don't really need the @line_data since we do nothing with it. With these considerations, we can shorten the code to

while ( my $line = <FILE> )
{
  push @lines_found, [ split " ", $line ]
    if $line =~ /$search_for/;
}

which is arguably pretty.

Sign up to request clarification or add additional context in comments.

4 Comments

My intention is to split a line(containing the search pattern) and keep it in an array. For the next line containing the search pattern it should split the same way and store it in some other array. So when I have 4 lines containing the search pattern I should have 4 different arrays for them for further operation.
@list_of_arguments[$count] = \@store; In my opinion this will not work for that
Opinions are like noses. Everybody has one. See my edit.
the perl script must be storing the split elements of each line in separate arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.