Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Skript help needed - RegEx & Hashes

by PandaRaey (Novice)
on Oct 15, 2018 at 16:45 UTC ( [id://1224047]=note: print w/replies, xml ) Need Help??


in reply to Skript help needed - RegEx & Hashes

Hello guys, thank all so, so much for your replies. I learned so much from these it's amazing haha.

I also apologize for not replying earlier, I had some major issues with the internet and were only able to get back on-line and take a look at your help today.

@Haukex; thank you very much. The reason I have been using the formatting as I did, is mainly because I constantly forget to open or close brackets and in the way I did it I have a bit better overview over the brackets I open and close. My guess is, that the more I work with perl and the more I will get used to it I can probably go back to a different formatting style.

Another question I would have would be about this: $h->{"3p-tRFs"} . How was it possible to replace the variable call from my version with " -> " ? My second question would be about the Data:Dumper. Isn't it contra-productive to use it when you are working with large data-sets and would end up printing a lot of content into the terminal? Or would you just use the Data:Dumper until you made sure that your script is working? I can definitely see the advantages of the Data:Dumper but I am just a bit worried it could slow things down too much with large data-sets.

poj, hippo, jwkrahn thank you all so much for your help and baring with my probably very basic and banal problems. But I definitely learned a lot from your help and I slowly start being less scared about working with perl.

  • Comment on Re: Skript help needed - RegEx & Hashes

Replies are listed 'Best First'.
Re^2: Skript help needed - RegEx & Hashes
by haukex (Archbishop) on Oct 15, 2018 at 19:59 UTC

    Glad to help, that's what we're here for :-)

    Like I said, don't worry too much about the formatting - it's much more important that you apply whatever formatting style you choose consistently, because no matter what formatting, if indentation isn't applied consistently, it's much easier to make mistakes. Using one of the more common styles might make your code a little easier to read to others, but inconsistent indentation is much more problematic.

    $h->{"3p-tRFs"} . How was it possible to replace the variable call from my version with " -> " ?

    The Arrow Operator is both the method call operator and the dereferencing operator. For example, I can say:

    my %hash = ( hello => "foo", world => "bar"); my $hashref = \%hash; # store a reference to the %hash print $hashref->{hello}; # prints "foo" $hashref->{world} = "quz"; # change "bar" to "quz" in orig. hash

    References are explained quite nicely in perlreftut - if you've heard of the concept of pointers, references are kind of like "safer" pointers, and therefore less scary ;-) Two advantages of references are that (a) instead of copying data structures when they are passed as arguments to functions*, you can just pass a reference instead, which saves memory and allows the function to modify the original data if desired, and (b) you can build complex data structures out of them, for example an array can contain a list of references to hashes, then you have an AoH (array of hashes); hash values can be references to arrays (HoA, hash of arrays), and all sorts of complex data structures. You can see plenty of examples of the latter in perldsc, and references are explained in detail in perlref.

    An "anonymous" hash or array is called that because it doesn't have a name. In "my $hashref = \%hash;", the hash being referenced by $hashref has a name, %hash. In "my $hashref = {};", this does basically the same as the previous piece of code, but now the hash referenced by $hashref is newly created and does not have a name (quite useful when building nested data structures).

    ... Data:Dumper. Isn't it contra-productive to use it when you are working with large data-sets and would end up printing a lot of content into the terminal?

    It's just intended for debugging output - I figured that's what you wanted because you were using prints in your original code. It's no problem to comment them out, or probably even better to do something like:

    my $DEBUG = 0; # at the top of the program ... $DEBUG and print Dumper(...); # or print Dumper(...) if $DEBUG;

    I personally prefer the former because it's visually a bit easier to skip those lines when you're skimming the code. Or, if performance is a concern, the following will be optimized away (the disadvantage being it's a bit trickier to change a constant via a command-line option):

    use constant DEBUG => 0; ... DEBUG and print Dumper(...);

    Minor edits for clarity.

    * Update 2: This statement applies when using the common ways to access arguments: sub abc { my ($foo,$bar,...) = @_ } and sub abc { my $foo=shift; my $bar=shift; ... }. Don't worry about accessing the elements of @_ directly just yet ($_[0], $_[1], ...), that's a topic for another day.

Re^2: Skript help needed - RegEx & Hashes
by Anonymous Monk on Oct 16, 2018 at 00:08 UTC
    > I constantly forget to open or close brackets

    You can get a wonderful Perl editor free from Activestate, which will insert a closing bracket any time you type an open bracket:

    www.activestate.com/komodo-ide/downloads/ide

      a lot of non commercial editors do that too
        > a lot of non commercial editors do that too

        Can you recommend any? I've been using Komodo trials since 2012 for free and don't really consider it to be too commercial. My favorite features are code completion, real-time error detection and Perl Critic integration. It's so good I plan to buy it for the advanced features and because I think that particular company deserves our support for supporting Perl so well for so long.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1224047]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found