Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I might write your code this way (but note I haven't tested it).

use strict; use warnings; my $param_file = 'Params.txt'; open my $param_fh, '<', $param_file or die "Can't read '$param_file': $!"; my (@params, @charam); while (<$param_fh>) { my ($name,$value) = split /:/; push @params, $name; push @charam, $value; } my @actual = @charam; close $param_fh or die "Close failed: $!";

Some differences from your version:

  • I Use strict and warnings (as Ratazong has already suggested).
  • I use three arg open for a little more safety.
  • When open fails, my error message includes $! so I know why, and it also says everything I'd want to know about the open that failed (what file I was opening and what mode I was trying to use—read).
  • The first argument to split is a regular expression.
  • Your $i and $j always have the same value, and you don't really need them since you're just appending to arrays. So I used push instead. That also means the first defined element of my array is 0 instead of 1.

The data you're reading would probably be best represented as a hash rather than a pair of arrays, but I say that without really knowing what you're going to do with it. If I were sticking all this in a hash, the code would look like this:

# same preamble my %value_of; while (<$param_fh>) { my ($name,$value) = split /:/; $value_of{$name} = $value; } # etc

Then you could get the stuff you have in @params and @charam using keys and values, but they'd be out of order.

If you're not familiar with hashes, I'd suggest you look into it. They are very useful.

I hope this helps.

In reply to Re: array print outside loop by kyle
in thread array print outside loop by shivakumar.balur

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-09-23 12:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found