Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

shifting data to the beginning of an array

by drock (Beadle)
on Jun 16, 2004 at 18:49 UTC ( [id://367370]=perlquestion: print w/replies, xml ) Need Help??

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

I have this code
## Set pragma use strict; ## Declare array, set scaler, read in file then my @ejectapes = qw(/usr/local/bin/perld/lvimgGH_ms0_tapes.orig); #($^I, @ARGV) = ('.bak', @ejectapes); open (FILE, "< @ejectapes") or die "cannot open @ejectapes: $!"; while (<FILE>) { chomp; print "$_ " if 1 .. 40; shift (@ejectapes, "eject 0,0,0"); } #s/(^E+)/eject\t0,0,0 \t$1/ #my $foo = "eject 0,0,0"; #my $a = shift(@ejectapes, $foo); close FILE;
what I want is from this output which looks like this: ere34 ere44 ddf344 rerd34 I want it to look like this on one line with spaces: eject 0,0,0 ere34 ere44 ddf344 rerd34

Replies are listed 'Best First'.
Re: shifting data to the beginning of an array
by Roy Johnson (Monsignor) on Jun 16, 2004 at 19:10 UTC
    unshift instead of shift.

    We're not really tightening our belts, it just feels that way because we're getting fatter.
Re: shifting data to the beginning of an array
by davido (Cardinal) on Jun 16, 2004 at 18:54 UTC
    One issue is that you're interpolating an array into a string and trying to use the results as a filename to open.

    If you intend to open multiple files, one after another, you should wrap your open.... while.... stuff in a foreach block that iterates over the filenames held in @ejectapes.

    Also, why are you setting @ARGV but then not using the magical properties of the <> operator? What exactly are you trying to do... maybe we can help you come up with a Perlish solution.


    Dave

      Dave, this is test code so let me explain... all I want it to do is insert the eject 0,0,0 string one time after the 40 lines are printed on one line, like so: eject 0,0,0 434 343 343 4232 2424 thanks!

Log In?
Username:
Password:

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

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

    No recent polls found