array elements

array elements

Post by Rekcuf Ykaen » Thu, 20 Feb 2003 20:42:12



Hello,

I am trying to read a file into a array of lines, so that I can
process them later on.

<CODE>

i=0
while read line ; do
 i=$((i + 1))
 ${lines[$i]}="$line"
done < $filename

</CODE>

However this does not seem to work.

Anyone with comments, pointers or suggestions ?

Thank you,

Reckuf

 
 
 

array elements

Post by Stephane CHAZELA » Thu, 20 Feb 2003 21:37:15



> I am trying to read a file into a array of lines, so that I can
> process them later on.

><CODE>

> i=0
> while read line ; do
>  i=$((i + 1))
>  ${lines[$i]}="$line"
> done < $filename

></CODE>

> However this does not seem to work.

You don't tell the shell you're using.

Assignment of array elements generally are as:
lines[i]=$line

If you're using zsh, you can do:

zmodload -i zsh/mapfile
lines=( ${(f)mapfile[$filename]} )

or

lines=( ${(f)"$(cat < $filename)"} )

With bash:

IFS=$'\n' eval 'lines=( $(cat < $filename) )'

With ksh:

IFS='
' eval 'set -A lines -- $(cat < $filename)'

A solution that would work for all of them
i=0
while IFS= read -r line; do
  i=$(( i + 1 ))
  lines[i]=$line
done < $filename

Note that neither Bourne nor POSIX shell support arrays. So you
shouldn't use them in a portable script.

--
Stphane

 
 
 

array elements

Post by Rekcuf Ykaen » Thu, 20 Feb 2003 21:50:47


On 19 Feb 2003 12:37:15 GMT, Stephane CHAZELAS



> > I am trying to read a file into a array of lines, so that I can
> > process them later on.

> ><CODE>

> > i=0
> > while read line ; do
> >  i=$((i + 1))
> >  ${lines[$i]}="$line"
> > done < $filename

> ></CODE>

> > However this does not seem to work.

> You don't tell the shell you're using.

Damn.. sorry, it's bash.

- Show quoted text -

Quote:> Assignment of array elements generally are as:
> lines[i]=$line

> If you're using zsh, you can do:

> zmodload -i zsh/mapfile
> lines=( ${(f)mapfile[$filename]} )

> or

> lines=( ${(f)"$(cat < $filename)"} )

> With bash:

> IFS=$'\n' eval 'lines=( $(cat < $filename) )'

> With ksh:

> IFS='
> ' eval 'set -A lines -- $(cat < $filename)'

> A solution that would work for all of them
> i=0
> while IFS= read -r line; do
>   i=$(( i + 1 ))
>   lines[i]=$line
> done < $filename

> Note that neither Bourne nor POSIX shell support arrays. So you
> shouldn't use them in a portable script.

 
 
 

array elements

Post by Rekcuf Ykaen » Thu, 20 Feb 2003 21:55:59


On 19 Feb 2003 12:37:15 GMT, Stephane CHAZELAS



> > I am trying to read a file into a array of lines, so that I can
> > process them later on.

> ><CODE>

> > i=0
> > while read line ; do
> >  i=$((i + 1))
> >  ${lines[$i]}="$line"
> > done < $filename

> ></CODE>

> > However this does not seem to work.

> You don't tell the shell you're using.

> Assignment of array elements generally are as:
> lines[i]=$line

> If you're using zsh, you can do:

> zmodload -i zsh/mapfile
> lines=( ${(f)mapfile[$filename]} )

> or

> lines=( ${(f)"$(cat < $filename)"} )

> With bash:

> IFS=$'\n' eval 'lines=( $(cat < $filename) )'

> With ksh:

> IFS='
> ' eval 'set -A lines -- $(cat < $filename)'

> A solution that would work for all of them
> i=0
> while IFS= read -r line; do
>   i=$(( i + 1 ))
>   lines[i]=$line
> done < $filename

> Note that neither Bourne nor POSIX shell support arrays. So you
> shouldn't use them in a portable script.

Eh.. ??
The bash man page:

Bash  is intended to be a conformant implementation of the IEEE POSIX
Shell and Tools specification (IEEE Working Group 1003.2).

 
 
 

array elements

Post by Stephane CHAZELA » Thu, 20 Feb 2003 22:19:49


[...]

Quote:>> Note that neither Bourne nor POSIX shell support arrays. So you
>> shouldn't use them in a portable script.

> Eh.. ??
> The bash man page:

> Bash  is intended to be a conformant implementation of the IEEE POSIX
> Shell and Tools specification (IEEE Working Group 1003.2).

Yes, that just means that POSIX scripts can be run by bash.

ksh, zsh (in POSIX emulation), newer ashes, bash all claim POSIX
compliance. All differ completely and have much more features
than what POSIX requires.

--
Stphane

 
 
 

array elements

Post by Greg Andre » Fri, 21 Feb 2003 02:07:20



>i=0
>while read line ; do
> i=$((i + 1))

I think you want  i=(($i + 1))

Quote:> ${lines[$i]}="$line"
>done < $filename

But what I do in my ksh scripts is this:

  while read line ; do

  done < $filename

It's only useful for adding elements to the end of
an array (like Perl's push), though.

  -Greg
--

     I have a map of the United States that's actual size.
                                -- Steven Wright

 
 
 

array elements

Post by Tim Cargi » Fri, 21 Feb 2003 03:46:13



> Hello,

> I am trying to read a file into a array of lines, so that I can
> process them later on.
[ ... ]
> Thank you,

> Reckuf

Check this recent thread:

http://groups.google.com/groups?dq=&hl=en&lr=&ie=UTF-8&frame=right&th...

Tim

 
 
 

array elements

Post by Greg Andre » Fri, 21 Feb 2003 04:03:28




>>i=0
>>while read line ; do
>> i=$((i + 1))

>I think you want  i=(($i + 1))

Oops.  I meant:  i=$(($i + 1))
I.e., you were missing the $ before the second i.

Also:  let i+=1
can also serve in ksh (don't know about bash)

  -Greg
--

     I have a map of the United States that's actual size.
                                -- Steven Wright

 
 
 

array elements

Post by Barry Kimelma » Thu, 27 Feb 2003 23:38:00




Quote:> Hello,

> I am trying to read a file into a array of lines, so that I can
> process them later on.

> <CODE>

> i=0
> while read line ; do
>  i=$((i + 1))
>  ${lines[$i]}="$line"
> done < $filename

> </CODE>

> However this does not seem to work.

> Anyone with comments, pointers or suggestions ?

> Thank you,

> Reckuf

You forgot to mention which shell you are using.

In any case I would recommend using Perl for this task.
Perl is widely available and very easy to use for this task.

#!/usr/bin/perl -w

$filename = $ARGV[0];
if ( ! open(INPUT,"<$filename") ) {
    die("Can't open $filename : $!\n");

}


close INPUT;


    # do something

Quote:}

--
---------

Barry Kimelman
Winnipeg, Manitoba, Canada

 
 
 

array elements

Post by Tim Cargi » Fri, 28 Feb 2003 05:26:18



> Hello,

> I am trying to read a file into a array of lines, so that I can
> process them later on.

[ ... deleted stuffs ... ]

This has been seen to work under pdksh, bash, and zsh.
I believe your previous problem was that the array loading
was occurring within a subshell, making the array
unavailble to the shell body:

#!/bin/bash

file=t1.txt

i=0

exec 3>&0 0<$file
while read line; do
        #echo "line = $line"
        i=$(( i + 1 ))
        lines[i]=$line
        echo "lines[i] = ${lines[i]}"
done
exec 0>&3 3>&-

j=0
echo "i = $i  j = $j"

while [ $j -lt $i ] ; do
        j=$(( j + 1 ))

        echo "$j ${lines[j]}"
done

Also, this site seems to have a good chapter on arrays:

   http://www.tldp.org/guides.html#abs

Quote:> Reckuf

Tim - PITA
 
 
 

array elements

Post by Tim Cargi » Fri, 28 Feb 2003 05:30:44





> > Hello,

> > I am trying to read a file into a array of lines, so that I can
> > process them later on.

[ ... ]

Quote:> You forgot to mention which shell you are using.

As long as it's not Bourne, it doesn't matter.
I just tested a simple array load/display that
compatible pdksh, bash, and zsh.

Quote:> In any case I would recommend using Perl for this task.
> Perl is widely available and very easy to use for this task.

Perl to load an array in a 'shell' environment?
How does that work?

Tim - PITA

 
 
 

1. Adding array elements perl

I have the following output from a command...Can I pipe it to a  single
line perl command to get the total (add each of them)....

4352
4160
1040
2112
8448
1040
2112
8448
1040
4224
2176

so perl output should be equal to 39152...

Thanks

2. RPC problems

3. Bash array, element breaked by space

4. ASCII > 127

5. array elements in (n)awk

6. /sbin/loader: Fatal Error: cannot map libm_c32.so

7. empty array element replacement

8. New CD-ROM Hangs Linux

9. printing array elements using printf

10. Referencing non-unique associative array elements

11. to shuffle of array element.

12. Parsing a line of array elements

13. ksh array element size?