1

I had got a script in Perl and my task is to do some changes in it. This of course means to understand which part does exactly what. I am not familiar with Perl language but I tried to read through some tutorials, but still some things are confusing me. And I got stuck in following part:

while (<KOEFICIENTYfile>) {
    @_=(split(/\s+/, $_));
    push(@ZAID,shift(@_));
    $KOEFICIENTY{$ZAID[-1]}=[@_];
}

As I understands this part then it:

  1. Reads line from filehandle KOEFICIENTYfile
  2. Separates them by spaces (one or more)
  3. Loads first item from this separated array into array ZAID (and in the process, removes it from @_)
  4. ??? Adds a rest of a separated array into array KOEFICIENTY? I am confused by curly brackets part and by square brackets after equals sign.

I think that I understood the meaning of @, $, @_ or negative indexing but this is beyond me. Can you please advice me on meaning of this?

1
  • All suggested answers was helpful and each had some interesting point in it, but as accepted answer i chosen @Disco 3´s answer since it was descriptive, helpful and suggested Data:Dumper. Anyway, thank you all. Commented Oct 12, 2015 at 9:43

3 Answers 3

1

[-1] indexing is just a shortcut way to say "last element of the array".

KOEFICIENTY is actually a hash (you can tell this because it is using curly braces, instead of square ones, around the index), so you're putting the rest of the array @_ into a hash called KOEFICIENTY with a key of the last element of the array.

If you include:

use Data::Dumper

at the top of the script and do

print Dumper(%KOEFICIENTY)

it will nicely format the output for you, which may help

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

Comments

1

The original coder was trying to be too clever using the negative offset. It would have been much more obvious if the code had been written with a simple temporary variable thus:

while (<KOEFICIENTYfile>) {
    @_ = (split(/\s+/, $_));
    my $key = shift(@_);
    push(@ZAID, $key);
    $KOEFICIENTY{$key} = [@_];
}

The braces on $KOEFICIENTY show that this is a "hash" of key/value pairs named %KOEFICIENTY, and not a normal array.

If you don't actually need to preserve the sort order of the keys you could just use keys %KOEFICIENTY to retrieve them instead of storing them in @ZAID.

1 Comment

So what are the braces? ({...})
0

@zaid is a list, into which the first part of the split is added.

%KOEFICIENTY is a hash, in which a reference to the rest of the split is stored as a list reference under the key of the first part.

So if the line is a b c, @zaid will get a and %KOEFICIENTY{'a'} will hold a reference to a list containing b and c.

Comments

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.