Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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
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 cooling their heels in the Monastery: (8)
As of 2014-09-22 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (189 votes), past polls