Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How to name arrays via scalars.

by hdb (Prior)
on May 03, 2014 at 10:04 UTC ( #1084880=note: print w/ replies, xml ) Need Help??


in reply to How to name arrays via scalars.

Due to a feature of Perl called Autovivification, you only need this:

use strict; use warnings; while(<FGLCS>){ chomp; my ($scfd_name_frgt,$stt_pst_frgt,$stp_pst_frgt,$lgh_frgt)=split /\t +/,$_; $scfd_name_frgt=reverse $scfd_name_frgt; push @{$frgtlocs{$scfd_name_frgt}},($stt_pst_frgt,$stp_pst_frgt,$lgh +_frgt); }

If @{$frgtlocs{$scfd_name_frgt}} does not exist, Perl just creates an array and you are done.

UPDATE: Also looking at these two lines:

@{$scfd_name_frgt}=($stt_pst_frgt,$stp_pst_frgt,$lgh_frgt); $frgtlocs{$scfd_name_frgt}=\@{$scfd_name_frgt};

They can be abbreviated using square brackets to create a reference to an array:

$frgtlocs{$scfd_name_frgt}=[$stt_pst_frgt,$stp_pst_frgt,$lgh_frgt];

There is no need to create a named array first and then store a reference to it (if that is what you want).


Comment on Re: How to name arrays via scalars.
Select or Download Code
Replies are listed 'Best First'.
Re^2: How to name arrays via scalars.
by qingdaoghawk (Initiate) on May 03, 2014 at 12:03 UTC

    Thanks very much. Your answer has directly solved my problem. I now understand more about the reference.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2015-07-29 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (267 votes), past polls