Skip to content

Problem with list ... #3306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
p5pRT opened this issue Jan 28, 2001 · 2 comments
Closed

Problem with list ... #3306

p5pRT opened this issue Jan 28, 2001 · 2 comments

Comments

@p5pRT
Copy link

p5pRT commented Jan 28, 2001

Migrated from rt.perl.org#5538 (status was 'resolved')

Searchable as RT5538$

@p5pRT
Copy link
Author

p5pRT commented Jan 28, 2001

From [email protected]

I want to break a long string in "file" to give me "file2" as output.
For this, I tried following code​:

### Start
open(fd,"file");
open(fd2,">file2");
@​array = <fd>;
$i = 0;
for ($i*70; $i < @​array/70; @​i++)
{
  @​temp = @​array[$i..$i+70];
  print fd2 "@​temp";
}
### End

To my surprise simply

print "@​array[1..10]";
OR
print "$array[1..10]";

were giving me the whole file NOT only 10 characters.

Whether I am doing wrong or a BUG in perl?

Perl Info

Flags:
    category=core
    severity=medium

Site configuration information for perl v5.6.0:

Configured by root at Thu Apr 27 15:49:46 PDT 2000.

Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration:
  Platform:
    osname=linux, osvers=2.2.14, archname=i386-linux
    uname='linux zap 2.2.14 #20 fri feb 25 19:44:02 pst 2000 i686 unknown '
    config_args='-de -Dprefix=/usr -Darchname=i386-linux -Dprivlib=/usr/lib/perl5 -Darchlib=/usr/lib/perl5/i386-linux -Dsitelib=/usr/lib/perl5/site_perl -Dsitearch=/usr/lib/perl5/site_perl/i386-linux'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=undef d_sfio=undef uselargefiles=define 
    use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=undef
  Compiler:
    cc='cc', optimize='-O2', gccversion=egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
    cppflags='-fno-strict-aliasing -I/usr/local/include'
    ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
    stdchar='char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lndbm -lgdbm -ldb -ldl -lm -lc -lposix -lcrypt
    libc=/lib/libc-2.1.2.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    


@INC for perl v5.6.0:
    /usr/lib/perl5/i386-linux
    /usr/lib/perl5
    /usr/lib/perl5/site_perl/i386-linux
    /usr/lib/perl5/site_perl
    /usr/lib/perl5/site_perl
    .


Environment for perl v5.6.0:
    HOME=/home/stu/b-99/u99103
    LANG (unset)
    LANGUAGE (unset)
    LC_ALL=POSIX
    LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib:/usr/local/oracle/OraHome1/lib
    LOGDIR (unset)
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/openwin/bin:/usr/games:.:/opt/kde/bin:/usr/share/texmf/bin:/usr/local/jdk1.2.2/bin:/usr/local/saclib2.1/bin:/usr/local/oracle/OraHome1/bin:/home/stu/subjects/IP/1999/:/usr/bin:/usr/local/bin:/usr/local/gnu/bin:.:/usr/local/games/bin:/home/stu/b-99/u99103/Misc/mybin:/usr/bin/X11
    PERL_BADLANG (unset)
    SHELL=/sbin/bash

@p5pRT
Copy link
Author

p5pRT commented Jan 28, 2001

From [Unknown Contact. See original ticket]

On 28 Jan 01, at 19​:53, u99103@​cs.unipune.ernet.in wrote​:

I want to break a long string in "file" to give me "file2" as output.
For this, I tried following code​:

### Start
open(fd,"file");
open(fd2,">file2");
@​array = <fd>;
$i = 0;
for ($i*70; $i < @​array/70; @​i++)
{
@​temp = @​array[$i..$i+70];
print fd2 "@​temp";
}
### End

To my surprise simply

print "@​array[1..10]";
OR
print "$array[1..10]";

were giving me the whole file NOT only 10 characters.

Whether I am doing wrong or a BUG in perl?

You appear to be misunderstanding some things about Perl.

I'll go through your code​:

### Start
open(fd,"file");
open(fd2,">file2");
@​array = <fd>;

This reads in the contents of "file", putting each line into a separate
array element of @​array. Lines will still contain their endings
(probably "\n").

$i = 0;

Sets $i to 0.

for ($i*70; $i < @​array/70; @​i++)

This does not compile for me at all. It gives me the warning "Can't
modify array deref in postincrement at -e line 1, near "@​i++"". Is this
really the code you used?

It calculates $i * 70 (which is 0 * 70 = 0, since you just set $i to 0)
and throws this result away. Then it continues while $i is less than
the number of lines in your file, divided by 70 (for example, if your
file had 50 lines, this would be true for $i == 0 but not for $i == 1).
So this is an infinite loop since you're not modifying $i anywhere.

{
@​temp = @​array[$i..$i+70];

This assigns elements from $i to $i+70 to @​temp. If $i is 0, then @​temp
receives the first 71 lines from file, from $array[0] to $array[70]. If
$i is 0, then @​temp receives $array[1] to $array[71].

print fd2 "@&#8203;temp";

And this prints the lines in @​temp to file "file2" separated by $",
which is usually a space. The result will be that all lines after the
first will begin with a space.

}
### End

To my surprise simply

print "@​array[1..10]";

This prints array elements from 1 to 10 (10 elements total), separated
by $" (usually a space).

OR
print "$array[1..10]";

Since "1..10" is in scalar context here, it's the "flip-flop" operator,
not the "range" operator, which is almost certainly not what you want
here. (`perldoc perlop`, section "Range Operators", for more
information on the flip-flop and range operators, which are both
spelled ".." (or "...").)

were giving me the whole file NOT only 10 characters.

If you read from the file with @​array = <fd>, then each element of
@​array is one line -- *not* one character. So @​array[1..10] gives you
the second to eleventh lines of the file.

If you want to have characters, you probably want something like
"substr" (or possibly "split //"). See `perldoc -f substr` and `perldoc
-f split` for more information on these functions.

If you can describe in more detail what it is exactly you are trying to
do, the answer can be more specific. However, I suggest you ask the
question on the newsgroup comp.lang.perl.misc, not with perlbug or to
perl5porters.

Cheers,
Philip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant