Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matrix with Character Values Possible?

Status
Not open for further replies.

androidmelt

Programmer
Mar 3, 2010
6
GB
Hi There,

Is it possible to have a matrix that holds character values?

I can manage a 3x3 matrix easily with the following command.
Integer :: matrix(3,3)

and I can for example, get an output like this:
3 6 1
6 2 1
9 5 2


Can a matrix hold characters too? I want, for example:
a b f
a a l
w i q


I can't obviously do that with an integer matrix because it keeps asking for integer inputs into the matrix.

Any ideas please?

Thanks :)
 
Yes, a matrix can hold chracters too:
Code:
[COLOR=#a020f0]program[/color] char_array
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

  [COLOR=#2e8b57][b]integer[/b][/color] :: i,j
  [COLOR=#2e8b57][b]character[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color]([COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]3[/color]) :: A

  [COLOR=#0000ff]! construct Matrix by columns and reshape[/color]
  A [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]reshape[/color](([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]'a'[/color],[COLOR=#ff00ff]'a'[/color],[COLOR=#ff00ff]'w'[/color],[COLOR=#ff00ff]'b'[/color],[COLOR=#ff00ff]'a'[/color],[COLOR=#ff00ff]'i'[/color],[COLOR=#ff00ff]'f'[/color],[COLOR=#ff00ff]'l'[/color],[COLOR=#ff00ff]'q'[/color][COLOR=#804040][b]/[/b][/color]), ([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color][COLOR=#804040][b]/[/b][/color])) 
  
  [COLOR=#0000ff]! print matrix by rows[/color]
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]3[/color]
    [COLOR=#0000ff]!print ('(3a2)'), (A(i,j), j = 1, 3)[/color]
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(3a2)'[/color]) (A(i,j), j [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]3[/color])
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end program[/color] char_array
Code:
$ g95 char_array.f95 -o char_array

$ char_array
 a b f
 a a l
 w i q
 
What's awesome? Like in another languages, you can define compound data types, e.g. arrays of every simple data types, e.g. integer, real, character,...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top