Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Use an incrementing counter as a variable (arrays)

by LanX (Saint)
on Sep 11, 2017 at 12:25 UTC ( [id://1199088]=note: print w/replies, xml ) Need Help??


in reply to Use an incrementing counter as a variable

I think what you rather want is an array called @bin

use strict; use warnings; my $counter=0; my @bin; while( my $line = <> ) { $counter++; $bin[$counter] = $line; }

see perldata#List-value-constructors for details on arrays (numbers as indices) and hashes (strings as keys)

and please always use

use strict; use warnings;

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: Use an incrementing counter as a variable (arrays)
by AnomalousMonk (Archbishop) on Sep 11, 2017 at 17:15 UTC

    Anonymous one: Variations on this approach are

    c:\@Work\Perl\monks\Anonymous Monk\1199083>perl -wMstrict -MData::Dump + -le "my @bin; ;; while(my $line = <>) { push @bin, $line; } ;; dd \@bin; " junk_1.txt ["line uno\n", "line two\n", "line 3\n"] c:\@Work\Perl\monks\Anonymous Monk\1199083>perl -wMstrict -MData::Dump + -le "my @bin = <>; ;; dd \@bin; " junk_1.txt ["line uno\n", "line two\n", "line 3\n"]
    where junk_1.txt contains
    line uno line two line 3
    Both these variations relieve you of the need to maintain the  $counter counter (assuming it's initialized to 0).


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-19 04:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found