Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

using arrays insteaed of files

by Anonymous Monk
on Jul 11, 2003 at 15:58 UTC ( [id://273434]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am trying to copy an @array instead of the file.dbf to the variable $database in order to use it in subroutine test().
Is this possible?
How can this be done?
Thanks
my @names=("Tin Grig","Karen Dnny","Lo Firo","Ben Maig","Luen Meza"); my $database = "test.dbf"; my $database = "@names"; test($database);

Replies are listed 'Best First'.
Re: using arrays insteaed of files
by davorg (Chancellor) on Jul 11, 2003 at 16:04 UTC

    It depends what you want to go into $database.

    $database = "@names"; # space separated list of names $database = @names; # number of elements in @names $database = \@names; # reference to @names $database = [@names]; # reference to a copy of @names
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thanks Dave This is what is in the test.dbf file. I think it is just a list. Tin Grig Lou Caero Karen Dnny Lo Fiadeiro Ben Mandig Luen Menoza
      I think this is what I meantthe file looked like.

      Tin Grig
      Lou Caero
      Karen Dnny
      Lo Fiadeiro
      Ben Mandig
      Luen Menoza
      I have tried all of the suggestions.
      $database = "@names"; # space separated list of names $database = @names; # number of elements in @names $database = \@names; # reference to @names $database = [@names]; # reference to a copy of @names
      But no data ever seems to be there.
        You say: "But no data ever seems to be there."

        What do you mean by "there"? All of the commands shown will put something into the variable $database if @names had something in it to begin with.

        The statement: my $database = "test.dbf"; looks like you were previously assigning a filename to $database. And that filename was then being passed to test(). Is that the case? If so, a routine expecting a filename is not going to do well if passed a string containing data instead.

        You seem to be at a beginning level so we will be much more able to help you if you tell us more specifically what you are doing and what you are asking for:

        • Where did you expect the data to be after you tried the statements above?
        • What did you do that convinced you that "no data ever seems to be there"?
        • In general: what are you *actually* doing (show us the code) and what are you seeing as a result?
        • What file are you refering to when you say "
        • What does the test() routine look like?
        • Did you have any success passing the value 'test.dbf' to the test() routine (before you tried setting $database to the array value)?

        NOTE: What often happens when you force yourself to *demonstrate* your problem to us instead of just reporting things like "it doesn't work" or "nothing was there" is that, in carefully laying out the specifics of your attempts you discover the solution for yourself. It's a good discipline.

        P.S. Good work on using "my". Is there a "use strict" at the top of your script also? And are you using warnings?

        ------------------------------------------------------------
        "Perl is a mess and that's good because the
        problem space is also a mess.
        " - Larry Wall

Re: using arrays insteaed of files
by naChoZ (Curate) on Jul 11, 2003 at 16:01 UTC
Re: using arrays insteaed of files
by dvergin (Monsignor) on Jul 11, 2003 at 16:57 UTC
    We need more information.

    It's hard to offer you help without knowing what the test() routine expects as input. If it is truly expecting a filename, then there is no simple way to fool it into accepting the data in another form.

    Show us the test() routine or tell us more about what it expects as input.

    Is the test() routine something you have control over (can you re-write it)? Or is it something you have to deal with as-is?

    Or... tell us what you are really trying to accomplish. We may be able to suggest a better way to approach the problem.

    ------------------------------------------------------------
    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall

      Hi, It looks like it is actually expecting a filename.
      open(DATABASE, "$database"); while (<DATABASE>) {}

      The file has a list of names. I guess the question how do I get the sub to look for an @array that contains a list of names instead.
        Excellent! Now we are getting somewhere...

        Next question: Are you free to modify the test() routine? If so, it would probably be good to show us the whole routine (if it is not too long).

        Or are you stuck with it as-is and asking how (without changing the test() routine) to get around its requirement for a filename when you want to pass it a string of data instead?

        UPDATE: As I understand your response below, you want to leave the test() routine untouched and get around its requirement for a filename so you can pass it a string of data instead.

        That is the messier option. I am out of time so I have to run. If I were faced with this and really needed to do what you describe, I would write a little routine that wrote an array to a file so I could do something like:

        test( array2file(@my_array) )
        Where array2file wrote the data to the file in proper form and returned the name of that file so that the test() routine would get what it needed to do its work.

        If that is *really* what you want, perhaps someone else will offer some code. Sorry, gotta run.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2025-02-08 19:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (95 votes). Check out past polls.