Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

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

Thanks very much for the feedback, everyone and sorry about any confusion, I hope my questions aren't trivial. I've 4 further questions on Revised Code Below, thanks very much in advance.

1. If I use $in on Lines 6 - 8 and print any elements, the entire lines of the file are returned without being processed (with pipes included). I thought I could substitute $_ with any variable. Why won't it work here?

2. If I use $_ on these lines instead, then I get good results (the specified element on Line 12.

3. If I UnComment Lines 9 - 11, a '1' is returned between the last 2 elements in each line. Why is that?

4. To clarify myself (on moritz's question), even-numbered elements return nothing, eg. if I type print $fields[0], "\r"; on line 12 in my revised script, I get nothing. I've found that any even number gives me nothing, it's odd, I thought the 0th element would be 'Baw', the 1st element would be 'Vao' and so on. The pipe is discarded by perl, so I don't understand why the even-numbered elements are 'skipped', so to print 'Baw', I type print $fields1, "\r" and to print 'Vao', I type print $fields3, "\r"

1 #!/usr/bin/perl -w 2 use strict; 3 4 open my $datfh, '<', 'C:\begperl\my scripts\cdata.txt' or die "Could +n't open file: $!\n"; 5 6 foreach my $_ (<$datfh>) { 7 my @fields = (map { s/^\s+//; s/\s+$//, $_ } # eliminates irr +elevant spaces 8 split /\|/, $_); 9 # for my $i (0 .. $#fields) { 10 # print $fields[$i], "\n" unless $fields[$i] =~ /\r|\n/; 11 # } 12 print $fields[1], "\r"; 13 } 14 close $datfh;

In reply to Re^2: reading array elements by jeah
in thread reading array elements by jeah

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (5)
    As of 2021-04-11 10:08 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found